Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Thanked 0 Times in 0 Posts

    Array input value coming out empty or null

    I am writing a very basic gradebook program that should ask user to enter the number of students and then do the following:

    Offer a menu that allows user to:
    -create grade book (based on number of students it will ask user to assign grades to each student id (0-arrGrades.lenght)
    -print gradebook
    -edit gradebook

    Everything in the code works fine except the most important part. When I initialize the variable arrGrades[intNumberOfStudents] and then try and populate the array with user input I get null or empty values when I try and println the values in the array. For some reason my input method isn't working.

    import java.util.*;
    public class GradeBook
      //declare all variable at class level
      static int intMenuChoice;
      static String[] arrMenu = {"Create Gradebook", "Print Gradebook", "Update Grade", "Exit"};//Declare and populate Menu
      static int intStudentCount;
      static Scanner console = new Scanner(System.in); //inititalize Scanner
      public static void main(String[] args) 
        System.out.print("Enter the number of students in your class: ");
        intStudentCount = console.nextInt();
        showMenu();//Show menu choices
      public static void runMenu() //Implement menu choices
        char[] arrGrades = new char[intStudentCount];
         if (intMenuChoice == 1)
         { // Create Gradebook  
           for(int i = 0; i< arrGrades.length; i++)
             System.out.print("Enter Grade for SID " + i + ":");
             arrGrades[i] = console.next().charAt(0); //why isn't it storing grade into array here?
         else if(intMenuChoice == 2)
         { //Print Gradebook
           for(int i=0; i< arrGrades.length; i++)
             System.out.println("SID " + i + ": Grade " + arrGrades[i]); //all grades are coming out empty
         else if(intMenuChoice == 3)
         {// Update Grade of particular student
           System.out.print("Enter a student ID to be updated (0-" + (arrGrades.length - 1) + ")");
           int intSelectedStudent = console.nextInt();
           System.out.print("Enter new grade for SID " + intSelectedStudent + ":");
           arrGrades[intSelectedStudent] = console.next().charAt(0);
           System.out.println("Bye Bye");
         showMenu();//menu called at the end of each task performed.
       public static void showMenu()
         for (int i = 0; i < arrMenu.length; i++)
          System.out.println(i+1 + ")" + arrMenu[i]);
         intMenuChoice = console.nextInt();
    I am at a VERY basic level here so I understand that the code is neither concise nor elegant but I really need to see why the array isn't populating.


  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    Its working fine except the single variable. You cannot put it there; the first line of the function will always create a new array, and since its local to the function runMenu it will also be destroyed after the runMenu is called and completed.
    Remove the arrGrades from the runMenu method and create a static member property for the arrGrades. Initialize it in the main and it will work as you expect.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts