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.

suroh
  • 917
  • 1
  • 10
  • 26
  • 1
    I think you should look into regex, that is a lot of code for simple task – The Law Feb 29 '16 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" – Ceelos Feb 29 '16 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 – suroh Feb 29 '16 at 18:38
  • Okay, thanks. That's easier to understand. – Ceelos Feb 29 '16 at 18:40

3 Answers3

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': ");
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 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 – suroh Feb 29 '16 at 18:39
  • @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. – brso05 Feb 29 '16 at 18:49
  • Yes it is the right answer, thank you @Elliott Frisch. I didn't even think about Regex – suroh Feb 29 '16 at 18:56
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.

Atri
  • 5,511
  • 5
  • 30
  • 40
  • 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 – suroh Feb 29 '16 at 18:44
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!

Community
  • 1
  • 1
Ceelos
  • 1,116
  • 2
  • 14
  • 35