0
import java.util.Scanner;
public class PlayAgain {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);           
    boolean playing = true;
    char replayCheck;

    do { //start do-while
        System.out.print("Play again? (y/n): ");           
        boolean validInput = false;
        while (validInput = false){ //start while
            replayCheck = input.next().charAt(0);
            switch (replayCheck) { //start switch
                case 'y':
                case 'Y':
                    validInput = true;
                    playing = true;
                    break;
                case 'n':
                case 'N':
                    validInput = true;
                    playing = false;
                    break;
                default:
                    System.out.println("Invalid input! please enter (y/n)");
                    validInput = false;
                    break;
            } //end switch
        } //end while
    } while (playing = true); //end do-while     
    System.out.println("Thanks for playing!");
} //end main
} //end class

If the user enters n/N the program plays again, same goes for any other input. The logic seems just fine, but I get "the assigned value is never used" on the line with replayCheck = input.next().charAt(0); so I suspect the issue is there.

I'm a bit of a noobie. Any suggestions are welcome!

Martin C.
  • 12,140
  • 7
  • 40
  • 52

3 Answers3

1

change '=' to '==' for comparison, and your code works fine :

import java.util.Scanner;
public class PlayAgain {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    boolean playing = true;
    char replayCheck;

    do { //start do-while
        System.out.print("Play again? (y/n): ");
        boolean validInput = false;
        while (validInput == false){ //start while
            replayCheck = input.next().charAt(0);
            switch (replayCheck) { //start switch
                case 'y':
                case 'Y':
                    validInput = true;
                    playing = true;
                    break;
                case 'n':
                case 'N':
                    validInput = true;
                    playing = false;
                    break;
                default:
                    System.out.println("Invalid input! please enter (y/n)");
                    validInput = false;
                    break;
            } //end switch
        } //end while
    } while (playing == true); //end do-while
    System.out.println("Thanks for playing!");
} //end main
} //end class
chenchuk
  • 5,324
  • 4
  • 34
  • 41
0

The check needs to be:

while (validInput == false) {
    ....
}

Otherwise you'll assign false to validInput, which results in false and therefore quits the loop.

In Java, the idiomatic way to write such a check is:

while (!validInput) {
    ...
}
Martin C.
  • 12,140
  • 7
  • 40
  • 52
0

Problem is at while loop it should be while (validInput == false) {}

Majeed Siddiqui
  • 548
  • 2
  • 9