[Solved] How to stop a while loop if the user entered a special character


After you have gotten the user input, you can see if the input equals “#” like so:

input.equals("#");

You can get the user input using Scanner:

Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();

You can break from a loop by using break.

The while loop would look like this:

while (true) {
    String input = scanner.nextLine();
    if (input.equals("#")) {
        break;
    }

    int number = Integer.parseInt(input);
    // do stuff
}

solved How to stop a while loop if the user entered a special character