0

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?

5
  • 1
    How about String names[]= name.split(" ") ? You are looking for space character by traversing the name character by character. Apr 28, 2018 at 2:05
  • The loop only runs once if the first character is a space, this is bad programming. The x = name.length() should be replaced with a break to make things more clear as that is essentially what it is acting as
    – Mitch
    Apr 28, 2018 at 2:10
  • This is code given to me as course material, the break does make more sense to me, does the if statement test one char at a time? Apr 28, 2018 at 2:12
  • yes the charAt function only looks at one character
    – Mitch
    Apr 28, 2018 at 2:12
  • May be you can use indexOf() to get the index of space and then the rest you know.. Apr 28, 2018 at 3:49

3 Answers 3

1

I do not know why you must be useing while code,maybe you can use name.split(" ") if you can sure that name ,useing " "separates first and last name,you can easy to separates it into first and last name.

0

Setting x to name.length() turns while condition x < name.length() to false, so it definitely does the trick: you will break out of the loop.

However, the code does not make clear the purpose of setting x to name.length(): even experienced coders would take a few seconds to figure out what you are trying to do. A better approach is to use break statement, which is specifically designed for ending the loop:

if(name.charAt(x) == ' ')
{
    firstName = name.substring(0, x);
    familyName = name.substring(x + 1, name.length());
    break;
}

Note that your code has two additional problems:

  • If the input does not contain a space, firstName and familyName remain unset, and
  • If the input contains a space as the last character, the code would throw an exception.
1
  • Thank you the break does make a lot more sense. Apr 28, 2018 at 2:24
0

This is the breakdown of the logic:

This would loop from x = 0 (assuming this is what it was initialized to) up to the length of the name string.

while(x < name.length())

This portion is entered upon finding the first space character.

if(name.charAt(x) == ' ')

This assigns the firstName and the familyName. firstName is from start of the name string to the character before the first space, familyName is from first character after space to the end of the name string. E.g. for Jane Doe, firstName is set to Jane and familyName is set to Doe.

firstName = name.substring(0, x);
familyName = name.substring(x + 1, name.length());

After the assignment, x is set to a condition which would not execute the while loop the next time.

x = name.length();

If a space was not encountered, x is incremented so that the next character in the name string can be evaluated.

++x;

A better way to do this is to write a break statement instead of setting x = name.length(). This will exit the while loop after the first space character is found and after the firstName and familyName name are set.

if(name.charAt(x) == ' ') {
  firstName = name.substring(0, x);
  familyName = name.substring(x + 1, name.length());
  break;
}
++x;
1
  • Ahh thank you, I was getting confused thinking that the code block in the if statement was being run each time, of course it doesn’t! Apr 28, 2018 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.