1

I have this simple code that works like a small command prompt. User is asked to enter a command and is displayed a message according to what he entered. I just want to make it loop so that the user is asked to enter a command again and again and again until he types 'exit'. How do I do that? Here is the code:

public static void main(String[] args) {

    Scanner command = new Scanner(System.in);

    System.out.println("Enter command: ");

    String text = command.nextLine();

        switch(text){

        case "start":
            System.out.println("Machine started!");
            break;

        case "stop":
            System.out.println("Machine stopped.");
            break;

        case "exit":
            System.out.println("Application Closed");
            break;

        default:
            System.out.println("Command not recognized!");
            break;
        }

    command.close();
}

Thank you

9
  • while ( not condition met ){ /* your code */ reset value to test } Commented Apr 27, 2015 at 13:09
  • Try to use chain if-else and use text.equals("xxxxxx") for the condition. Commented Apr 27, 2015 at 13:16
  • @farukdgn: why would he do that? Commented Apr 27, 2015 at 13:19
  • @Stultuske stackoverflow.com/a/513839/3025112 Commented Apr 27, 2015 at 13:20
  • @farukdgn: you are testing for the wrong value. "xxxxxxxx" and "exit" will never be equal. I was, however, referring to your if-else proposal. Commented Apr 27, 2015 at 13:23

5 Answers 5

5

I prefer:

public static void main(String[] args) {

    Scanner command = new Scanner(System.in);

    System.out.println("Enter command: ");
    boolean running = true;

    while(running){

        switch(command.nextLine()){

        case "start":
            System.out.println("Machine started!");
            break;

        case "stop":
            System.out.println("Machine stopped.");
            break;

        case "exit":
            System.out.println("Application Closed");
            running = false;
            break;

        default:
            System.out.println("Command not recognized!");
            break;
        }
    }
    command.close();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Constantly re-declaring text? Why not just once?
@Stultuske How's that bad? Keeping references within their own scope.
@Bubletan: it was more a question than a remark. considering the scope here: String text = ""; while ( .... ){ /* code */ } Yes, limiting to the scope is not bad, but re-creating the instance variable each time, there are those who don't like to see that either.
@Stultuske Yah, best way would be just switch (command.nextLine()).
3

You can put it in a while loop like this:

String text = "";

while(!text.equalsIgnoreCase("exit"))
{
    System.out.println("Enter command: ");

    text = command.nextLine();

        switch(text){

        case "start":
            System.out.println("Machine started!");
            break;

        case "stop":
            System.out.println("Machine stopped.");
            break;

        case "exit":
            System.out.println("Application Closed");
            break;

        default:
            System.out.println("Command not recognized!");
            break;
        }
}

Just let the condition for your while loop be while text <> "exit".

Comments

1

Something like this? I prefer do-while for this type of situation because you probably want to give at least one command.

public static void main(String[] args) {
    boolean running = true;

    Scanner command = new Scanner(System.in);
    do {
        System.out.println("Enter command: ");
        String text = command.nextLine();
        switch(text){
            case "start":
                System.out.println("Machine started!");
                break;
            case "stop":
                System.out.println("Machine stopped.");
                running = false;  //here?
                break;
            case "exit":
                System.out.println("Application Closed");
                running = false; //..or here?
                break;
            default:
                System.out.println("Command not recognized!");
                break;
        }
    } while(running);
    command.close();
}

Comments

0

If you want to use a switch, easiest way would probably be a simple infinite while-loop with a label. When the input matches "exit", we just break the loop.

loop:
while (true) {
    String text = command.nextLine();
    switch (text) {
    // ...
    case "exit":
        break loop;
    }
}

Comments

0

while loop is missing in the initial example:

    public static void main(String args[]) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (true) {
        String command = br.readLine().toLowerCase();

        switch (command) {
            case "exit": {
                // exit here
            } break;

            default: {
                // unrecognised command
            }
        }
    }
}

1 Comment

You misspelled siwtch. Also, you're never leaving the while-loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.