user615599
user615599

Reputation: 11

How to get empty input from the user in Java

How do I get user input while asking the user Do you want to continue or not?

If the user press Enter then how do I go to the next instructions so I compare it to do other tasks?

Upvotes: 0

Views: 652

Answers (1)

user613857
user613857

Reputation:

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

Edit: Sorry, misread question (well question title and body is different). You can ask the user like this:

System.out.print("Do you want to continue? ");
Scanner scanner = new Scanner(System.in);

if (scanner.next().toLowerCase().equals("y")) {
    // continue...
} else {
    // don't continue
}

So when a user types in "y" or "Y" it will continue. Otherwise, it won't.

Upvotes: 2

Related Questions