Scanner input validation in while loop

前端 未结 3 1516
北海茫月
北海茫月 2020-11-29 13:07

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 h

3条回答
  •  感情败类
    2020-11-29 13:57

    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
    }
    

提交回复
热议问题