2 Replies - 396 Views - Last Post: 16 April 2013 - 04:35 PM Rate Topic: -----

#1 totmore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 13

Help with while loop?

Posted 16 April 2013 - 04:27 PM

I have this program that asks the user for their name, I want to use a while loop to check if the user entered a first and last name or a first, middle and last name; basically if the user enters only one name it should show an error like wise if the user enters more then three names(first,middle,last) i want to show an error. My while loop seems to not do the trick any insights please?

this is the loop:

while(numWords < 2 || numWords > 3)
       {
           System.out.println("Error Enter 2 or 3 words");
            System.out.println();
            
            System.out.print("Enter a name: ");
            name = sc.nextLine();
       }


 public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Welcome to the name parser.\n");
        String choice = "y";
        
        while(!choice.equalsIgnoreCase("n"))
        {
        System.out.print("Enter a name: ");
        String name = sc.nextLine();  
               
        String[] nameParts = name.split(" ");
        int numWords = nameParts.length;
        name = name.trim().replaceAll("\\s+", " "); 
            

       while(numWords < 2 || numWords > 3)
       {
           System.out.println("Error Enter 2 or 3 words");
            System.out.println();
            
            System.out.print("Enter a name: ");
            name = sc.nextLine();
       }
        

        String firstName = "";
        String middleName = "";
        String lastName = "";
        
        
        if(nameParts.length == 3)
        {
            firstName = nameParts[0];
            middleName = nameParts[1];
            lastName = nameParts[2];
        }
          if(nameParts.length == 2)
            {
                firstName = nameParts[0];
                lastName = nameParts[1];
            }
            
         
       if (firstName != "")
           System.out.println("First Name: " + firstName);
        if (middleName != "")
           System.out.println("Middle Name: " + middleName);
         if (lastName != "")
           System.out.println("Last Name: " + lastName);
        

       System.out.println();
       System.out.println("Would you like to enter another name? y/n");
       choice = sc.nextLine();  
     }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help with while loop?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Help with while loop?

Posted 16 April 2013 - 04:32 PM

Moved to Java. Please do not post help questions in the Caffeine Lounge.
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,028
  • Joined: 11-December 07

Re: Help with while loop?

Posted 16 April 2013 - 04:35 PM

Your loop depends on the value of numWords but that variable never changes inside the loop. It's going to loop forever! Presumably, after you read the line of user input but before the end of the loop, you need to recalculate how many words there are.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1