10

I've got to show Scanner inputs in a while loop: the user has to insert inputs until he writes "quit". So, I've got to validate each input to check if he writes "quit". How can I do that?

while (!scanner.nextLine().equals("quit")) {
    System.out.println("Insert question code:");
    String question = scanner.nextLine();
    System.out.println("Insert answer code:");
    String answer = scanner.nextLine();

    service.storeResults(question, answer); // This stores given inputs on db
}

This doesn't work. How can I validate each user input?

2
  • Not sure for how many its the same case, but this actually worked properly for me. Not sure what exactly didn't work for you. Remember to give "quit" only and not any other case versions of it. Commented Nov 13, 2013 at 10:10
  • When asking about something that "doesn't work", specify in what way it doesn't work. How did its behavior differ from what you expected? Commented Jun 1, 2018 at 12:53

3 Answers 3

11

The problem is that nextLine() "Advances this scanner past the current line". So when you call nextLine() in the while condition, and don't save the return value, you've lost that line of the user's input. The call to nextLine() on line 3 returns a different line.

You can try something like this

    Scanner scanner=new Scanner(System.in);
    while (true) {
        System.out.println("Insert question code:");
        String question = scanner.nextLine();
        if(question.equals("quit")){
            break;
        }
        System.out.println("Insert answer code:");
        String answer = scanner.nextLine();
        if(answer.equals("quit")){
            break;
        }
        service.storeResults(question, answer);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Why is it that the Scanner scanner = new Scanner(System.in) is outside the while loop?
@drewteriyaki: You don't want a new Scanner created for every user input. A single Scanner on the single system input stream keeps a consistent state on that stream.
4

Try:

while (scanner.hasNextLine()) {
    System.out.println("Insert question code:");
    String question = scanner.nextLine();
    if(question.equals("quit")){
     break;
    }

    System.out.println("Insert answer code:");
    String answer = scanner.nextLine();

    service.storeResults(question, answer); // This stores given inputs on db
}

3 Comments

What's the difference between while (scanner.hasNextLine()) and while (true) (like in Ruchira's answer) in this case?
while(true) will terminate only in break and while(scanner.hasNextLine()) terminate at EOF.
But how can it reach the end of file if there will always be a scanner.nextLine() (even if empty)?
0

always check if scanner.nextLine is not "quit"

while (!scanner.nextLine().equals("quit")) {
    System.out.println("Insert question code:");
    String question = scanner.nextLine();
    if(question.equals("quit"))
     break;

    System.out.println("Insert answer code:");
    String answer = scanner.nextLine();
    if(answer.equals("quit"))
      break;

    service.storeResults(question, answer); // This stores given inputs on db 

}

Comments

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.