0

I am attempting to write a program with specification for a Beginners Java class. The code is below. There is one line that is not compiling about 30 lines in inside the first for look of the first do while loop near.

score[i + 1][i] = input.nextDouble();

This is the line of code exactly. It wont compile and it wont give any kind of errors so I don't know what I've done wrong. I am at a bit of a loss at the moment, could use a second pair of eyes. Just to explain what I did wrong so I can fix it and how I should have identified it.

THESE ARE THE SPECIFICATIONS FOR THIS ASSIGNMENT *************************************************************************. The menu system will have an option to input grades for the next student. Once pressed the user will then enter how many exams that student has taken. The program will then ask the user to enter each of those exam scores. Menu will have an option to display the exam average by student. Menu will have an option to display the exam average by exam. Menu will have an option to display the current class average for all exams. *************************************************************************.

import java.util.*;

public class JaggedArray {


public static void main (String[ ] args) {
    int numStudents = 0, count = 0, x = 7;
    int numGrades = 0;
    String choice = "";
    int vId[];
    double score[ ][ ] = null;
    Scanner input = new Scanner (System.in);

    System.out.println("Welcome. How many students do you want to record grades for today?");

    numStudents = input.nextInt();
    vId = new int[numStudents];
    for (int s = 0; s < vId.length; s++) {
        System.out.println("Please enter the students class ID.");

        vId [s] = input.nextInt();

        System.out.println("How many exam grades would you like to record for this student?");

        numGrades = input.nextInt();
        do {
            System.out.println("Enter all exams taken for this student.");
            for (int i = 0; i < numGrades; i++)     {           
                System.out.print("\tSTUDENT # " + (count + 1) + " \tEXAM # " + (i + 1) + " : ");
                score[i + 1][i] = input.nextDouble();
            }
            count++;
            System.out.println("Is there additional students you would like to add? (y/n) : ");
            input.nextLine().toLowerCase();
            choice = input.nextLine(); 
            } while (choice.equals("y"));

        }


    //menu loop
    do {
        try {   //will try and catch to prevent input error by user, sets x to different value if correct
        System.out.println("*****************MENU*****************");
        System.out.println("1.\t Display the average of all exams by each Student ID.");
        System.out.println("2.\t Display the average of each exam by Exam #.");
        System.out.println("3.\t Display the current class average for all exams for all students.");
        System.out.println("**************************************");
        System.out.println("\n");
        System.out.print("Please enter your choice: ");
        int menuOption = input.nextInt();

        //average of all exams by student
            if (menuOption == 1) {
                for (int i = 0; i < count; i++) {
                    int studentSum = 0;
                    for (int j = 0; j < numGrades; j++) {
                        studentSum += score[i][j];
                    }   //end of inner for loop
                    double studentAvg = (double) studentSum / numGrades;
                    System.out.println("\nStudent #" + (i + 1) + " : " + studentAvg + "\n");
                }   //end of outer for loop

                //average of all exams by exam #
            } else if (menuOption == 2) {
                for (int i = 0; i < numGrades; i++) {
                    int examSum = 0;
                    for (int j = 0; j < count; j++) {
                        examSum += score[j][i];
                    }   //end of inner for loop
                    double examAvg = (double) examSum / numGrades;
                    System.out.println("\nExam #" + (i + 1) + " : " + examAvg + "\n");
                }   //end of outer for loop

                //current class average for all exams for all students
            } else if (menuOption == 3) {
                int classSum = 0;
                for (int i = 0; i < count; i++) {
                    for (int j = 0; j < numGrades; j++) {
                        classSum += score[j][i];
                    }   //end of inner for loop
                }   //end of outer for loop

                double classAvg = (double) classSum / (numGrades * count);
                System.out.println("\nAverage : " + classAvg + "\n");
            } else {
                System.out.println("Invalid choice");
            }   //end of  if-else statement

            //ask to select from menu again
            System.out.print("Would you like to continue (y/n) : ");
        input.nextLine().toLowerCase();
        choice = input.nextLine();
        x = 1;
        }   // end try and on to catch
        catch (Exception e) {
            System.out.println("Sorry, you can't do that. Try again.");
        }   // end catch
        }   // end do-while
     while (choice.equals("y") && (x == 1));
    System.out.println("Thank you!");
    }   //end of main
}   //end of class
iAm Bob
  • 1
  • 1
  • What do you mean "It wont compile and it wont give any kind of errors"? If it doesn't compile Java always gives you an error message – Sacha Jun 04 '18 at 02:29
  • 1
    Do you mean you get a `NullPointerException`? You initialize `score` to `null` and never assign anything else to it. – Ted Hopp Jun 04 '18 at 02:31
  • Sorry, i mean prior to running. After running, it takes the inputs then spits out this message Exception in thread "main" java.lang.NullPointerException at JaggedArray.main(JaggedArray.java:30) – iAm Bob Jun 04 '18 at 02:31
  • You never initialize `score`. It can only be null at that point. – Dmich Jun 04 '18 at 02:35

0 Answers0