Daniel
Daniel

Reputation: 45

Grade Calculator Does Not Loop to Second Student

I saw a recent post that is literally trying to achieve the same thing as me, but his code stops at one student and it won't work. I managed to fix what was wrong with his code but there are two things that I still don't understand. When I input the student's name with a first and last name the java code has an error and the program stops. When I use just a first name the program goes all the way through but stops after I calculate one of the student's grades. It does not continually loop until the number of students I've entered have all their grades.

public class proj2
{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        System.out.println("Welcome to Gradecalculator!");

        System.out.println("Please enter the number of students:");
        int students = s.nextInt();

        System.out.println("Please enter the number of exams:");
        int exams = s.nextInt();

        int i = 0;
        int studentnumber = 1;

        int sum = 0;
        while (i < students) {

            System.out.println("Enter student " + studentnumber++ + "'s name :");
            String studentname = s.next();

            System.out.println("Enter exam scores :");

            for (; i < exams; i++) {
                int n = s.nextInt();
                sum+=n;

                if (n < 0) {
                    System.out.println("Invalid exam scores, reenter: ");
                }
            }
            double average = sum/exams;
            if (average <= 100 && average >= 90) {
                System.out.println("Letter grade: A");
                System.out.println(studentname + " gets 4 stars! ****");
            }
            if (average <= 89 && average >= 80) {
                System.out.println("Letter grade: B");
                System.out.println(studentname + " gets 3 stars! ***");
            }
            if (average <= 79 && average >= 70) {
                System.out.println("Letter grade: C");
                System.out.println(studentname + " gets 2 stars! **");
            }
            if (average <= 69 && average >= 60) {
                System.out.println("Letter grade: D");
                System.out.println(studentname + " gets 1 star! *");
            }
            if (average <= 59) {
                System.out.println("Letter grade: F");
                System.out.println(studentname + " gets 0 stars!");
            }
        }
    }
}

Here is my code, and my objective is to have the program loop again once I've already obtained my output for student one, but it just stops at averaging the grade for the first student.

Upvotes: 1

Views: 1207

Answers (3)

jpw
jpw

Reputation: 44891

When I input the student's name with a first and last name the java code has an error and the program stops

I'm guessing that the error you're getting is java.util.InputMismatchException. This happens becauseString studentname = s.next();only gets one token, and when you enter a name with two tokens likeJohn Doe, then second token remains in the input buffer and is parsed by the next callint n = s.nextInt();which of course won't work. Instead of usingString studentname = s.next();you want to read the entire line withString studentname = s.nextLine(); (the best option might be to use Integer.parseInt() for all the calls when you want to read numbers.)

Also, when you read data usingnextInt()the newline character won't be consumed which means that when you go on to the next call it's still there and will be parsed instead of the name (in this case). Either read an empty line usings.nextLine()before you read the name, or useInteger.parseInt(s.nextLine())to read the data.

You might also want to use exceptions to catch errors due to bad input gracefully.

On a side note class names in Java should start with a capital letter as stated in the Code Conventions for the Java Programming Language

See the documentation for Scanner.nextLine().

Also, as noted in another answer and what I missed is that you're using the same variableiin both the while loop that gets student names and the for loop that gets scores for each student. This won't work. You should use another variable likejin the second loop:

for (int j = 0; j < exams; j++) {
                int n = s.nextInt();
                sum+=n;

                if (n < 0) {
                    System.out.println("Invalid exam scores, reenter: ");
                }
            }

Ideally you'll want to use am object oriented approach and create student objects with associated test scores and read data into an collection (maybe an arrayList) of students, but maybe that's is a solution at a later stage in the course or exercise path you seem to be on.

Upvotes: 2

learningCurve
learningCurve

Reputation: 109

It looks like you are doing the While loop then the for loop then the if and back to the for loop till it finishes all the grades. you need to put both the student name and grade in the same for loop.

 for(i=0; i<3; i++){
    System.out.println("What is the Students name?");
    Name = kb.next();//NAME
    System.out.println("What is the Students test score?");
    Grade = kb.nextDouble();//GRADE
    System.out.println();//SKIP SPACE ON SCREEN
    student[i]= new TestScores(Name, Grade); //PASSES ARGUMENT THEN STORES NAME AND GRADE WITH ARRAY ADDRESS  
   } 

    kb.close(); 

Upvotes: 1

Jim Lewis
Jim Lewis

Reputation: 45105

You are using i as in index to both the outer (students) and inner (exams) loops. You should use separate variables to ensure that the inner loop doesn't interfere with the outer loop.

Upvotes: 1

Related Questions