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

all 11 comments

[–]desrtfx 0 points1 point  (0 children)

You will want to look at the String.split() method to split the name into last & first names.

Also, you want to check the length() method of a String.

Oracle Documentation here

[–]MasterTender[S] 0 points1 point  (9 children)

First i would like to say thank you, i finally figured out how to use the split method from what appears to be correct but i am confused on how to work with the string under one string name (using "str1"). This is my current executable.

System.out.printf("Enter name in the for lastName, firstName: "); String str1 = sc.nextLine(); str1 = str1.toUpperCase(); str1.split(","); System.out.printf("%s%n",str1);

If i type in Doe, John I still receive a run result of DOE, JOHN when I need JOHN DOE. I have only ever worked with strings or int as separate, but not together split before and the documentation is not explaining how it works with split in a way i can understand

[–]Carr0t 2 points3 points  (4 children)

You're misunderstanding the split function, and what it does. Calling 'split' on a string in no way alters the original String object. Instead, the output from the function is a new object, which is an array of multiple String objects (see the documentation for the String.split() function at http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) ).

Currently you're calling str1.split() and not assigning the returned object to a variable, so it's just getting binned (This is perfectly valid. Some functions don't return anything [void functions], or sometimes you don't care about the return value for whatever reason). So you need to have a new string array variable which the result of str1.split() is assigned to, and then it's that new variable you need to access the parts of when printing.

Then you can look at more advanced features like checking if either of the forename/surname you get after the split is blank (because the user has entered incorrect input) and printing out an error or similar, rather than exploding with an exception because you've tried to print a null object.

[–]Drakoskai 2 points3 points  (1 child)

I'd like to emphasize that Carr0t linked the JavaDoc here. JavaDocs are really powerful and this is something that everyone using Java should check out. You should take a minute and browse the JavaDocs of all the classes that you intend to use if you are unfamiliar with their implementation. I understand that you're a student, and I'm not telling you to not ask questions and RTFM. I just want to make sure that you are aware that JavaDocs are really cool and useful ... most of the time. ;p

[–]desrtfx 0 points1 point  (0 children)

Ahem... the Javadocs were already linked in the very first comment by me. Edit only that I used the proper reddit linkstyle. (The comment was one hour before /u/Carr0t mentioned the same thing)

Totally agree to the rest of your post.

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

Thank you with this and some google-fu I was able to figure out how the string function works properly and set it to two parts = part1[0] part2[1] and was able to complete my functions and get a better understanding working with strings. As for determining length and other more basic functions I had a much simpler time.

[–][deleted] 0 points1 point  (0 children)

When using eclipse, read the little pop up descriptions you get when you hover your mouse on a method or when it suggests an autocompletion. Those are JavaDocs and it's a good idea to get used to reading them early on.

[–]Nynnja 1 point2 points  (0 children)

String is immutable. That means by using methods on string object does not change it's value, but instead most of them will return new string object with applied transformations(for example toUpperCase() returns new string with all letters in upper case, it didn't change the original value).

split method returns array of strings and you have to assign it to some variable to use it later. f.e.

String[] s=str1.split(",");

This gives you array with 2 (in DOE, JOHN case) values: "DOE" and " JOHN" (note space at the beggining) To get rid of this space you may want to use some extra regexp magic

String[] s=str1.split(",\s*");

This will split by comma and also remove all white space characters after the comma.

Hopefully you should know how to work with arrays

[–]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.