0

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?

6
  • can any body plz look at my question Commented Feb 14, 2011 at 1:41
  • 1
    What do you want the user to enter if he does not want to continue? Commented Feb 14, 2011 at 1:49
  • i just wanna know if i be taking integer input then if user want to skip that and press enter then i have to ask him next question ..... Commented Feb 14, 2011 at 1:50
  • 2
    Consider re-writing your question and putting in enough information so that we can fully understand it, because right now it's somewhat confusing. One link that has helped me learn to ask answerable questions is: How To Ask Questions The Smart Way Commented Feb 14, 2011 at 2:47
  • 1
    @user615599 - 1) if you don't take the time to write coherent questions, you are unlikely to get the answers that you need. 2) most SO folks don't respond positively to newcomers demanding answers; e.g. "I JUST WANNA KNOW ...". Commented Feb 14, 2011 at 3:16

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

6 Comments

I JUST WANNA KNOW like String a = scan.next();
If you just want to know, then you already know it: String userResponse = scanner.next();. Your question title, body and your comments do not match, they ask for different things. Sorry, this is all I can do.
amazed you could do that much given the nature of the question. 1+ for trying.
Now that I re-read my answer, I figured if (!...) { // stop the code } would be a better way to do it. Thanks for the +1, did my best.
My guess is he needs a while loop with an if block inside of it -- the while loop to repeat, the if block to set the boolean that is tested in the while's condition block.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.