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

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,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 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,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 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
    472
    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.

  9. #9
    Junior Member
    Join Date
    Jul 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I loop a switch Statement?

    package com.company.project;

    import java.util.Scanner;

    public class application {

    public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    boolean runAgain = false;
    boolean exitloop = false;

    do {

    System.out.println("Enter the command here, valid values are stop, start, to exit program type 'exit' : ");
    String input = scanner.nextLine();

    switch (input) {

    case "start":

    System.out.println("System has been started");
    break;

    case "stop":

    System.out.println("system has been stopped");
    break;

    case "exit":
    System.out.println("system has been stopped");
    exitloop = true;
    break;

    default:

    System.out.println("Command not recognized");
    runAgain = true;

    }

    if (exitloop)
    break;

    } while (runAgain);

    scanner.close();

    }

    }

  10. #10
    Junior Member
    Join Date
    Aug 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I loop a switch Statement?

    Hi Arkeshen,

    Here is a solution that I could work out, please let me know if I did not understand the requirements properly or if anything is not clear:

    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("Program restart"); //<---- program will restart if user enters 1, 2 or 3
                        //Here you need to replace the print statement with the actual code that will restart your program or do what you want to do
                    case 4:
                        System.exit(0); //<----- program will terminate if user enters 4
                    default:
                        System.out.println("No such Parameter"); //<----- if user enters anything other then 1, 2, 3 or 4 it prints "No such Parameter"
                }
            }
        }
    }


    Regards
    Alin

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I loop a switch Statement?

    This thread is very old. Closing.

    Also read: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

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 11th, 2010, 12:31 AM
  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