View Single Post
Old 12-10-2012, 06:43 AM   PM User | #1
togipaw
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
togipaw is an unknown quantity at this point
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
-exit

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.

Code:
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.println();
       System.out.print("Enter new grade for SID " + intSelectedStudent + ":");
       arrGrades[intSelectedStudent] = console.next().charAt(0);
     }
     else
     {
       System.out.println("Bye Bye");
       System.exit(0);
       
     }
     System.out.println("________________________");
     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();
     System.out.println();
     runMenu();
    }
}
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.

Thanks
togipaw is offline   Reply With Quote