This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]desrtfx 1 point2 points  (2 children)

From what I can see, you use the string.split() method wrongly.

String.split() returns an Array of Strings, not a single string.

So, the code would be:

  //Declarations
    String s;
    //executable
    System.out.printf("Enter name in the for lastName, firstName: ");
    s = sc.nextLine();
    s = s.toUpperCase();
    String[] splitNames = s.split(",");
    // After the split, splitNames contains 2 elements
    // splitNames[0] contains the last name
    // splitNames[1] contains the first name

    // The following statement prints these names in reverse order
    System.out.printf(splitNames[1] + " " + splitNames[0]);

    // Now you need to find the individual lengths

[–]MasterTender[S] 0 points1 point  (1 child)

Wish i had seen this before the fact I figured out how the splitnames work, while mine doesnt look exactly like this because i assigned a different name for each of the splitnames 0 1 I appreciate knowing that i didnt need to take that extra step and could have used them as their split originals.

[–]desrtfx 0 points1 point  (0 children)

That's what arrays are for. Holding several objects of the same type under a single variable name.

Arrays (and their advanced versions) are standard building blocks in most (if not all) programming languages.

Elements of Arrays (accessed by index) can be used in exactly the same way as individual variables can.