J. Lars
J. Lars

Reputation: 9

How can I make a command in java execute only once?

I'm a complete beginner at Java and right now I'm trying to learn more about switch/class/loops and I have this code:

boolean loop = true;
while (loop) {
    Scanner input = new Scanner(System.in);
    boolean executed = true;

    if (!executed) {
        System.out.println("\nPlease enter a command: ");
        executed = true;
    }

    String text = input.nextLine();

    switch (text) {

        case "start":
            System.out.println("\nYou began playing");
            text = input.nextLine();
            //loop = false;

        case "stop":
            System.out.println("\nYou stopped playing");
            text = input.nextLine();

    }
}

My problem is that I am trying to have this line: System.out.println("\nPlease enter a command: "); only run once, how do I do that?

When I type "start", I get the start message, and when I type "stop", I get the stop message. But if I type "start" or "stop" a second time, it jumps to the "please enter a command" message.

How do I change the code so that it doesn't run the first message the second or third time I type in "start" or "stop"?

I took someone's advice and added this:

if (!executed) {
    System.out.println("\nPlease enter a command: ");
    executed = true;
}

But this doesn't work, it only results in the first message not showing up at all, when the program starts...

Upvotes: 0

Views: 3121

Answers (2)

Christopher Jung
Christopher Jung

Reputation: 103

boolean executed = true;

if(!executed) {
    System.out.println("\nPlease enter a command: ");
    executed = true;
}

is equivalent to

if(!true) {
    System.out.println("\nPlease enter a command: ");
    executed = true;
}

So the statement will never enter. You have to put your variable outside of the while loop and set it to false.

boolean executed = false;
while(loop) {

    Scanner input = new Scanner(System.in);


    if(!executed) {
        System.out.println("\nPlease enter a command: ");
        executed = true;
    }

    //...
}

But your problem is, that you never leave the while loop after "stop" .

Additionally you forgot the break after each switch statement. That cause in a continue with the next switch statement.

Better write

boolean loop = true;

Scanner input = new Scanner(System.in);

while(loop) {
    System.out.println("\nPlease enter a command: ");
    String text = input.nextLine();

    switch (text) {

    case "start":
        System.out.println("\nYou began playing");
        break;

    case "stop":
        System.out.println("\nYou stopped playing");

        loop = false;
        break;
    }
}

If you really want to call the "Please enter a command" only once put it outside of the while loop.

Upvotes: 1

Lino
Lino

Reputation: 19926

You can improve your code by a lot, as currently you have some major flaws in it.

Scanner input = new Scanner(System.in);
System.out.println();
System.out.println("Please enter a command: ");

outer: for(String text = input.nextLine(); ; text = input.nextLine()) {   
    switch (text) {    
        case "start":
            System.out.println();
            System.out.println("You began playing");
            break;
        case "stop":
            System.out.println();
            System.out.println("You stopped playing");
            break outer; // break the for-loop
        default: 
            System.out.println();
            System.out.println("Invalid input");
    }
}
  1. You can move the line System.out.println("\nPlease enter a command: "); out of the loop.
  2. You can replace every System.out.println("\nSome message") with just two calls to System.out.println() one is empty one has the message.
  3. You probably want to break in your switch statements. Because else they will fall-through, leading to not intended behaviour.
  4. You also want to add a default branch in your switch to handle cases when text will not be equal to "start" nor "stop".

Upvotes: 1

Related Questions