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

kiko
  • 23
  • 1
  • 4

5 Answers5

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();
}
Praeterii
  • 340
  • 6
  • 16
  • Constantly re-declaring text? Why not just once? – Stultuske Apr 27 '15 at 13:21
  • 1
    @Stultuske How's that bad? Keeping references within their own scope. – Bubletan Apr 27 '15 at 13:26
  • @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 Apr 27 '15 at 13:31
  • @Stultuske Yah, best way would be just `switch (command.nextLine())`. – Bubletan Apr 27 '15 at 13:34
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".

brso05
  • 13,142
  • 2
  • 21
  • 40
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();
}
Frigo
  • 284
  • 4
  • 15
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
            }
        }
    }
}
ioseb
  • 16,625
  • 3
  • 33
  • 29
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;
    }
}
Bubletan
  • 3,833
  • 6
  • 25
  • 33