Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 12-10-2012, 01:14 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
array, null, user input

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:32 PM.


Advertisement
Log in to turn off these ads.