9

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?

Kurt Bourbaki
  • 9,882
  • 6
  • 28
  • 46
  • 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. – RainMaker Nov 13 '13 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? – LarsH Jun 01 '18 at 12:53

3 Answers3

10

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);
    }
LarsH
  • 25,732
  • 8
  • 77
  • 136
Ruchira Gayan Ranaweera
  • 32,406
  • 16
  • 66
  • 105
  • 1
    Why is it that the `Scanner scanner = new Scanner(System.in)` is outside the while loop? – drewteriyaki Nov 08 '17 at 00:08
  • @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. – LarsH Jun 01 '18 at 12:54
3

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
}
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 

}

mosaad
  • 1,893
  • 4
  • 20
  • 46