Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Switch statement inside a for loop need help.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch statement inside a for loop need help.

    Problem:
    my output is the following:

    Enter the type of Person: S
    Enter in a name: John Down
    Enter the type of Person: Enter the type of Person:


    The output I would like to see is....
    Enter the type of Person: S
    Enter in a name: John Down
    Enter the type of Person:


    I'm trying to figure out how to use a switch statement inside of the for loop. I'm trying to figure out how to force the loop to stop so it will allow me to answer the prompt, "Enter the type of Person:"

    Any suggestions?

    for(int i=0; i<4; i++)                                                                      
    		{                                                                                         
    			System.out.print("Enter the type of Person: ");                             
    			userInput = keyboard.next().charAt(0);
     
     
    		switch(userInput)                                                                         
    			{                                                                                         
    				case 'P':                                                                             
    					String name; 
    					System.out.print("Enter in a name: ");                                          
    					name = keyboard.next();                                                       
    					people[1].setName(name);                                                          
                        break;                                                                            
    				case 'S':


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Switch statement inside a for loop need help.

    Scanner.next() returns the next token, without eating the next line that comes after it. So when you get the character entered, the next token available is the next line that's still there. You then set your name to that next line and keep on trucking.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  3. What can be legally inside FOR statement?
    By ice in forum Loops & Control Statements
    Replies: 7
    Last Post: January 13th, 2011, 12:31 PM
  4. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  5. How to Use the Java switch statement
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 5
    Last Post: October 8th, 2009, 09:00 PM