I am having trouble with some String methods. In the code below a user enters their full name and the code separates it into first and last name.
while(x < name.length())
{
if(name.charAt(x) == ' ')
{
firstName = name.substring(0, x);
familyName = name.substring(x + 1, name.length());
x = name.length();
}
++x;
}
I am having trouble understanding what the requirement of the while loop is, as I read it, x is assigned to the name.length in the if statement which would set x == name.length() meaning the loop would only run once???
Can someone please help me break this down?
String names[]= name.split(" ")
? You are looking for space character by traversing the name character by character.x = name.length()
should be replaced with abreak
to make things more clear as that is essentially what it is acting ascharAt
function only looks at one character