March 12th, 2013, 02:49 PM
Split 1 String into 2 seperate strings
I need help figuring out how to split 1 unknown string Ex: "Firstname Lastname" into two seperate strings String a: "Firstname" String b: "Lastname"
March 12th, 2013, 02:55 PM
Look at split() in String
March 13th, 2013, 02:01 PM
Still stuck with trying to print out the second part of the string (last). All I get is the first name, if anyone can see what's wrong with my code or have any pointers would be great
public class YourName
public static void main(String  args)
Scanner kb = new Scanner(System.in);
System. out. println ("What is your Name?");
name = kb.next();
String  data = name.split(" ");
first = (data);
last = (data);
System.out.println(first + last);
March 13th, 2013, 03:04 PM
Remember next() will just give you the next token, a String. Have you tried nextLine()?
March 13th, 2013, 04:29 PM
What is the value of last when you execute the code?
I would expect an ArrayIndexOutOfBoundsException: 1
error with the posted code.
March 13th, 2013, 10:01 PM
Bullet - thanks for the suggestion, finally works