-2

I am a beginner to java and trying to implement a do while loop for a number of cases within a switch statement.

    do {

        switch (UserInput){
        case "a2":
        case "a3":
        case "b1":
        case "b2":
        case "b3":
        case "c1":
        case "c2":
        case "c3":
        case "d1":
        case "d2":
        case "d3":
            TextIO.putln("This is a valid move!");
            break;
            default:
                TextIO.putln("Not a valid choice, please try again!");
        }
        } while (UserInput!="a2");

However when the choice is valid, it is constantly repeating 'This is a valid move' and vice versa for when it's invalid. Can anyone help with this?

JB1000
  • 1
  • 4
  • what exactly do you want to do? – Abhishek May 02 '16 at 13:04
  • 2
    Well, at the end of the loop you need to get another input. Otherwise it's going to continue using the input that you received before the loop beginning. – Evan Bechtol May 02 '16 at 13:06
  • Stop the messages for either a valid move or and invalid move from repeating down the page when the program is executed. – JB1000 May 02 '16 at 13:06
  • What do you mean get another input? As i said complete beginner here. – JB1000 May 02 '16 at 13:07
  • I'm assuming that `UserInput` is a scanner that is used to gather input from the user in some form. So use that variable to get a second input at the end of the loop. You need to create a scanner: `Scanner input = new Scanner(System.in);` then you can use it to store user input. – Evan Bechtol May 02 '16 at 13:10
  • btw. its convention in Java that variable names start with a lower case character. – Rhayene May 02 '16 at 13:14
  • There are two reasons: One has to do with your input (or rather, the *lack* of input) and the other with how you compare strings. – Some programmer dude May 02 '16 at 13:15
  • see also: [how do I compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Rhayene May 02 '16 at 13:18
  • Managed to get it working. Thanks everyone. – JB1000 May 02 '16 at 13:20

3 Answers3

1

I'll give an example with Scanner(System.in):

Scanner s = new Scanner(System.in);
String input = "";

do {
    input = s.next();

    switch(input) {
    case "42":
        System.out.println("Success");
        break;
    default:
        System.out.println("Wrong answer!");
    }
} while(!input.equals("42"));

s.next() is a blocking call (in this case), that means program execution is halted while s.next() waits for new user input.

Without that call, the while loop would just keep evaluating the same value of input over and over again, spamming to the console.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
0

It's because the user input has to be set within the loop, so the user is able to type in something else. You're just setting the user input once, iterating over it again and again and again...

Tobias Brösamle
  • 598
  • 5
  • 18
0

You aren't getting another input to be validated before the loop-condition is evaluated each iteration. You need to use a Scanner to gather another input that the loop condition can then validate.

 Scanner scn= new Scanner(System.in);
 do {

    switch (UserInput){
    case "a2":
    case "a3":
    case "b1":
    case "b2":
    case "b3":
    case "c1":
    case "c2":
    case "c3":
    case "d1":
    case "d2":
    case "d3":
        TextIO.putln("This is a valid move!");
        break;
        default:
            TextIO.putln("Not a valid choice, please try again!");
    }

    // We get the new input that we can check for validity
    UserInput = scn.nextLine();
    } while (!UserInput.equals("a2"));
Evan Bechtol
  • 2,855
  • 2
  • 18
  • 36