Java Help

Soldato
Joined
27 Aug 2004
Posts
17,111
Location
Geordieland
Im stumped on part of my java homework at uni and could do with a hint on what i need to do to be honest.

Anyways, i need to figure out how to seperatea a single input string, more specifically a name into 2 words. The name is stored as a string in a driver class and needs to be seperated into the first and last name and then printed as 2 seperate strings, but ill be damned if i can think how get java to find the space in the string.

Only thing i can actually think of doing is to put it into a while/for loop ad go 1 by 1 along the charAt until it finds a char position with no value contained in it, with anything behind the space being word 1 and after the space word 2, but im probably way off the mark.

Also, does anyone have any good java tutorial websites? My lectorals are useless and never really covers things properly. Was fine in semester 1, but sem 2 is getting very difficult.

Cheers for any help
 
Last edited:
Use a string tokenizer

Code:
import java.util.StringTokenizer;

String fullName, firstName, secondName;

//Code to set fullName to the desired value name

StringTokenizer st = new StringTokenizer(fullName);

firstName = st.nextToken();
secondName = st.nextToken();
The default delimiter is the space character so setting fullName to "John Smith" and running this code will set firstName to "John" and secondName to "Smith"

As for tutorials the Java api is always your friend (link)
 
Last edited:
I remember an almost identical example being used at uni, I will point you in the right direction but you will learn more if you experiment yourself. Apologies for the cheesy phrase.

You want to use 2 of the String class methods: indexof(" ") which will return the character number of the space and substring(int, int) which will return the portion of the string between the first into and the second int.

RTM if confused: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

As for the tutorials you requested, I don't know about you but I work best from well written books however the sun tutorials are pretty informative:

http://java.sun.com/docs/books/tutorial/

Good luck and I hope your not attempting this at this time of night :D .
 
MaxPower said:
Good luck and I hope your not attempting this at this time of night :D .

no no, not this late. Trying to read up on it to try to figure out hints as to how to do it, gonna do it tomorrow.

I will point you in the right direction but you will learn more if you experiment yourself.

Thats all i was after, a bit of hinting as to how to start :)

CHeers for the help guys, much appreciated! :D
 
Code:
  String name = "john smith";
    
  String[] tokens = name.split(" ");

  System.out.println(tokens[0]); // Prints john
  System.out.println(tokens[1]); // Prints smith.

As lagz says just read the API, really simple stuff.

Stellios said:
My lectorals are useless and never really covers things properly. Was fine in semester 1, but sem 2 is getting very difficult.

Cheers for any help

PS. Where are you Newcastle or Northumbria? :p
 
Got the majority of it working now using the tokenizer, handy function that is. Gonna have a look at the other suggested ways too, the more i know the better :)

Thanks for the help to everyone, wouldnt have known where to start otherwise :)
 
Last edited:
always always always start with the API descriptions.

they are all online and searchable.

If you comment your own code with javadoc readable comments you can even generate the same format of documentation for your own code which rocks in a huge way.

I 'think' that the sting.split() method is faster than creating a tokenizer

enjoy
 
Back
Top Bottom