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 8 of 8

Thread: How do I loop a switch Statement?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I loop a switch Statement?

    How do I loop a switch Statement?
    I need to loop it so that when a user inputs a 1, 2, 3, or anything other than a 4 it restarts, and when the user types in a 4 the program terminates.

    import javax.swing.JOptionPane ;
    public class Input123
    {
    public static void main(String[] args)
    {
    int n;
    n = Integer.parseInt(JOptionPane.showInputDialog(null,
    "Enter a 1, 2, 3, or 4" )) ;
    {
    switch(n)
    {
    case 1:
    case 2:
    case 3:
    System.out.println("Good Job!") ;
    break ;
    case 4:
    System.out.println("Pragram Terminated") ;
    break ;
    default:
    System.out.println("No such Parameter") ;
    }
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: How do I loop a switch Statement?

    What you can do is place your switch inside a while loop => while(flag). Then inside the case 4 set the flag to false.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I loop a switch Statement?

    Quote Originally Posted by Junky View Post
    What you can do is place your switch inside a while loop => while(flag). Then inside the case 4 set the flag to false.
    Can you elaborate?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: How do I loop a switch Statement?

    Use a boolean.
    boolean foo = true;
    while(foo) {
        //stuff
        if( x == 4) {
            foo = false;
        }
    }
    Above example uses an if but you can easily apply it to a switch

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I loop a switch Statement?

    Quote Originally Posted by Junky View Post
    Use a boolean.
    boolean foo = true;
    while(foo) {
        //stuff
        if( x == 4) {
            foo = false;
        }
    }
    Above example uses an if but you can easily apply it to a switch
    It's compiles with no syntax errors, but it's still givng me an infinite loop where when I put in a 1, 2, 3, or any number other not 4 it outputs either "Good Job!" or "Parameter Error". So I probally have it wrong somewhere.

       import javax.swing.JOptionPane ;
       public class Input123
       {
          public static void main(String[] args)
          {     
             int n;
             n = Integer.parseInt(JOptionPane.showInputDialog(null,
                   "Enter a 1, 2, 3, or 4" )) ;
             {
                boolean loop = true;
                while(loop) 
                { 
                   switch(n)
                   {
                      default:
                         System.out.println("Parameter Error") ;
                         break ;
                      case 1:
                         System.out.println("Good Job!") ;
                         break;
                      case 2:
                         System.out.println("Good Job!") ;
                         break;
                      case 3:
                         System.out.println("Good Job!") ;
                         break ;
                      case 4:
                         System.out.println("Pragram Terminated") ;
                         break ;
                   }        
                   if(n == 4) 
                   {
                      loop = false;
                   }
                }
             }
          }
       }

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How do I loop a switch Statement?

    I suggest you put the input dialog inside the loop, so it prompts for input each time round...

    Incidentally, by convention, 'default' is the last item in a 'switch', the idea being that all values needing individual handling are checked first, and the default takes effect for all other values. This also means that by omitting the 'break' immediately above, execution will 'drop through' to the default handler.
    Last edited by dlorde; May 25th, 2011 at 02:48 PM.

  7. #7
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: How do I loop a switch Statement?

    Yea he's right. You definitely need the input inside the while loop or its going to only ask once and keeping going with that same input forever.

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I loop a switch Statement?

    What you want to do is move the loop so the input and the switch statement is contained within it.
    Easiest way would be to make it a while loop with the condition true, but give the loop a label and to exit just break that loop as I have edited your code to do below.

    [highlight = Java]
    edited by moderator - please read http://www.javaprogrammingforums.com/cafe/9544-problem-spoon-feeding.html
    [/highlight]
    Last edited by copeg; July 19th, 2011 at 08:56 AM.

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. Java Program Help Switch Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2010, 11:31 PM
  3. Advancing day w/ if/else & switch statement
    By rbread80 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 10:43 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. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM