0

I've been trying to debug my code for a long time, and still couldn't figure out where is the mistake, could you help me, please? the program adds users input into the map until user answer "no" to the question " Do you want to add more students?" however whatever the user enters "yes" or "no" the program stop running. There could be an issue with the do/while loop, I'm not sure

 //there is swith statement above and HashMap<String, ArrayList> students = new HashMap()
 case 2:
 ArrayList<Integer> scores = new ArrayList<>();
 String name;
 String answer;
 do {
     System.out.print("Please enter student's name: ");
     name = in.nextLine();
     in.nextLine();
     System.out.print("Please enter " + name + "'s scores: ");
     scores.add(in.nextInt());
     System.out.println("Do you want to add more students yes/no?");
     answer = in.nextLine();
     in.nextLine();
 } while (answer.equals("no"));
 students.put(name, scores); //adding names and scores into HashMap<String, ArrayList> students = new HashMap()

 if (answer.equals("no")) {
     System.out.println("Student Quiz Scores");
     keys.forEach((i) -> {
         System.out.println(i + "'s quiz scores: " + students.get(i));
     });

 }
Gayan Mettananda
  • 1,207
  • 13
  • 15
  • You have a lot of calls to `nextLine()`, but you have `answer = in.nextLine()` right after you call `nextInt()`. This will resolve an empty `String` to `answer`, the loop condition will become false, the `if` will not be entered and the program will end – GBlodgett Dec 07 '18 at 04:10
  • Also I think you wanted `while(!answer.equalsIgnoreCase("no"));` – GBlodgett Dec 07 '18 at 04:11

1 Answers1

0

use answer=in.next() instead of answer=in.nextLine().

next() = Finds and returns the next complete token from this scanner.A complete token is preceded and followed by input that matchesthe delimiter pattern.

nextLine() = Advances this scanner past the current line and returns the inputthat was skipped.