I want to read from the keyboard the first name and last name of a member separated by space I have tried the following but it doesn't work.

public static void updateWeight()
    {
        Scanner in = new Scanner(System.in); // Creation of a new Scanner object
        String tempName = ""; // Store temporary name collected by Scanner objecgt
        String tempLastName = ""; // Store temporary last name collected by Scanner object
        boolean found = false; // Boolean value in the case tha a member is found
        int i = 0;
        
        //Message to be displayed on the screen to input the values of tempName and tempLastName
        System.out.println(" Give first name and last name separated with a  (space) of the member you want to change the weight.");
        String input = in.nextLine();
        String delims = "[ }";
        String tokens = input.split(delims);
        tempName = in.next();
        tempLastName = in.next();
incompatible types

String tokens = input.split(delims);

Thanks for any help.

I want to read from the keyboard the first name and last name of a member separated by space I have tried the following but it doesn't work.

public static void updateWeight()
    {
        Scanner in = new Scanner(System.in); // Creation of a new Scanner object
        String tempName = ""; // Store temporary name collected by Scanner objecgt
        String tempLastName = ""; // Store temporary last name collected by Scanner object
        boolean found = false; // Boolean value in the case tha a member is found
        int i = 0;
        
        //Message to be displayed on the screen to input the values of tempName and tempLastName
        System.out.println(" Give first name and last name separated with a  (space) of the member you want to change the weight.");
        String input = in.nextLine();
        String delims = "[ }";
        String tokens = input.split(delims);
        tempName = in.next();
        tempLastName = in.next();
incompatible types

String tokens = input.split(delims);

Thanks for any help.

Im not sure what this is ment to do"

String delims = "[ }";

but if you want to split a string by a whitespace do:

input.split(" ");

Edited 4 Years Ago by DavidKroukamp: n/a

Its too simple
String.split("(Space)") // Leave a space here ... Second split function returns a String array so u need a String array to carry the values.... Like

String a[] = String.Split(" ");

And then print them or use them for ur logic...

Hope this Help....

This article has been dead for over six months. Start a new discussion instead.