0

I would like to make the user input a grade only between 0-100 but I would also like for their to be an error message thrown if they enter a letter/word when asked for a numeric grade.

I have tried to make it loop and continually ask the user to input the grade, but when I run the program the exception is not caught and the run-time error still happens.

Can anyone explain why this is happening and guide me on how to fix it, here is my code:

System.out.println("Enter Grade #3 for: " + sArray[x].studentName);
boolean loop = false;
while (!loop) {
  try {
      int temp3 = kbReader.nextInt();
      while (temp3 < 0 || temp3 > 100) {
          System.out.println("Error: Please enter a valid grade between 0-100:");
          temp3 = kbReader.nextInt();
      }
      if (temp3 >= 0 && temp3 <= 100) {
          sArray[x].grade3 = temp3;
          loop = false;
      }
  } catch (Exception e) {
      System.out.println("Error please enter an integer!");
      kbReader.reset();
      kbReader.nextInt();

  }
}
Brien Foss
  • 3,336
  • 3
  • 21
  • 31
Alex G
  • 17
  • 6
  • Why do you call `kbReader.nextInt();` in the `catch` block, although you already know the next token isn't an int? – Tom Feb 03 '18 at 03:51
  • I tried to used .next() but the program does not let me go past the error message if I do this – Alex G Feb 03 '18 at 03:54
  • It's then waiting for your new input for `temp3` ... – Tom Feb 03 '18 at 03:56

0 Answers0