-1
public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    System.out.println("Welcome to GradeCalculator!");
    System.out.println("\nPlease enter the number of students: ");
    int numberOfStudent = s.nextInt();
    System.out.println("Please enter the number of exams: ");
    int numberOfExams = s.nextInt();
    System.out.println();

    //outer loop for the number of students
    for (int i = 1; i <= numbeOfStudent; i++) {
        System.out.println("----------------------------------------");
        System.out.println("Enter student " + i + "'s " + "name: ");
        String name = s.nextLine();
        s.next();
    }
    System.out.println();

    //inner loop for the number of exams scores entered
    int sum = 0;
    for (int j = 1; j <= numberOfExam; j++) {
        System.out.print("Enter exam scores: ");
        double examScore = s.nextDouble();
        sum += examScore;

        if (examScore < 0) {
            System.out.println("Invalid exam scores, reenter: ");
            double examScoreReenter = s.nextDouble();
            sum += examScoreReenter;
        } else {
            System.out.println();
        }
    }
}

console output :

Welcome to GradeCalculator!

Please enter the number of students: 
2
Please enter the number of exams: 
3

----------------------------------------
Enter student 1's name: 
john smith
----------------------------------------
Enter student 2's name: 
jane smith

Enter exam scores: "get exception"
------------------------------------------------------------------------

I've been struggling with this for days now and I cannot figure it out. The output I want is this:

-------------------------
Enter student 1's name : 
Enter exam score:
Invalid exam scores, reenter:
-------------------------

Any suggestions would be greatly appreciated.

Tiny
  • 27,221
  • 105
  • 339
  • 599
java
  • 1
  • 1
  • 1
  • 1
  • 4
    Your inner loop is not actually inside your outer loop. – Joe DeRose Oct 10 '14 at 03:47
  • If I take the } after s.next() then it'll be in the outer loop, right? – java Oct 10 '14 at 03:58
  • Even then, this follows the Enter exam scores: output "Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at test.main(test.java:31)" – java Oct 10 '14 at 03:59
  • Compiletion error @java have a look at it. – Naveen Kulkarni Oct 10 '14 at 04:22
  • @java Have a look at [this](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) regarding that InputMismatchException. The linked question uses `nextInt()` and `nextLine()` as an example, but the same thing can happen with `next()` and `nextLine()` too. – Dennis Meng Oct 10 '14 at 06:38

1 Answers1

0

If you enter a non-valid double, such as "thisisastringnotanumber", an InputMismatchException is thrown. You should surround your nextDouble() call with a try block to accomidate for this.

for (int j = 1; j <= numberOfExam; j++) {
    System.out.print("Enter exam scores: ");
    try{
        double examScore = s.nextDouble();

        if (examScore < 0) {
            System.out.println("Invalid exam scores, reenter: ");
            j--; //Retry this iteration
        } else {
            sum += examScore;
            System.out.println();
        }
    }catch(InputMismatchException e){
        System.out.println("Invalid exam scores, reenter: ");
        j--; //Retry this iteration
    }
}
Mshnik
  • 7,032
  • 1
  • 25
  • 38