1

I want to write a loop that prompts the user to input their first middle and last name, I then want to validate that input by searching for the white spaces in between each name.

Example: First Middle Last

What I'm looking for is some thing like the following.

Pseudo Code: if name contains 2 white spaces and their are less than 3 white space in name the operation has been successful other wise tell the user to re-input their first middle and last name.

How can I go about doing this?

import java.util.Scanner;

public class Example 
{
   public static void main(String[] args)
   {
       boolean isName = false; 
       String name = "";
       int x = name.length(); 
       Scanner input = new Scanner(System.in);

       while(!isName) // Probably better to remove the while loop entirely
       {  
          System.out.print("Please input your 'First Middle Last' name: ");
          name = input.nextLine(); 
          name.trim(); // To remove any leading or trailing white spaces

          for(int i = 0; i < x; i++)
          {
             if(name.lastIndexOf(' ', i) == 2 && name.lastIndexOf(' ', i) < 3)
             {          

                isName = true;
                break; 
             }

               else 
                System.out.print("\nEnter your name as 'First Middle Last': ");
                name = input.nextLine();
                name = name.trim();
                System.out.print("\nInvalid input");            

          }
       }
    }
 }

The above produces an infinite loop and logically I understand why.

4
  • 1
    I think you should look into regex, that is a lot of code for simple task Commented Feb 29, 2016 at 18:30
  • 1
    I'm having a hard time understanding: "if name contains 2 white spaces and their are less than 3 white space in name the operation has been successful" Commented Feb 29, 2016 at 18:36
  • How so? It's Psuedo code? If the person enters Thomas [space] Jones [space] then [Smith] the input is correct, if the user inputs [thomas] space and then hits enter then the input would be incorrect. I need at least two white spaces Commented Feb 29, 2016 at 18:38
  • Okay, thanks. That's easier to understand. Commented Feb 29, 2016 at 18:40

3 Answers 3

1

You could split your String on one or more white space characters and check that you get three elements. Something like,

boolean isName = false;
String name = "";
Scanner input = new Scanner(System.in);

while (!isName) {
    System.out.print("Please input your 'First Middle Last' name: ");
    name = input.nextLine();
    isName = (name.trim().split("\\s+").length == 3);
    if (!isName) {
        System.out.print("\nEnter your name as 'First Middle Last': ");
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

It's not == 3 it's < 3, it's already set to check if it's == 2 and the way Iv'e written now wont work any way lol
@Afflicted when you split by space the returned array should have 3 elements (names) first, middle, and last. That is why it's ==3 because if you have 2 spaces then you have 3 names which is what you want.
Yes it is the right answer, thank you @Elliott Frisch. I didn't even think about Regex
0

Here is the problem (infinite loop):

for(int i = 0; i < x; i++)

x is initialized to 0 because name.length() is 0 initially. Since the condition i<x is never satisfied, it never enters the for loop and the while loops goes on for ever.

Right before the for loop, you need to do x = name.length(). Also, as others have suggested you need to enclose the statements inside {} for the else part.

1 Comment

Thanks for pointing that out I can't believe I put that up there, sadly even if it's initialized in the correct location (as in after the string is input) it still wont work because it logically can't with the methods in the if
0

As per this link you can count white spaces with:

 int numOfSpaces = s.length() - s.replaceAll(" ", "").length();

With this you can tell if you have at least 2 spaces. The link also goes over different methods of counting how many white spaces exist in a given String.
Cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.