I am in beginning Java and using Dr. Java to write my programs. I am trying to write an array that user input sets up the length of the array(#of students) that character grades are stored in. It is suppose to ask if you want a full print out, to update a students grade, or to exit. please let me exactly what is that I am doing wrong, I did include the error codes it is throwing, making it impossible to test accurately.

Thank you for your help:

import java.io.*; // import needed files
import java.util.*; // import more needed files

public class Grades{
    public static void main (String []args){

int m, sid, asid, y, id, counter; //declare int variables


      SimpleInput keyboard = new SimpleInput (); // for user input

        System.out.println("Greetings, welcome to the class grade tracking system. ");
        System.out.println("How many students are in the class?"); 


          id = keyboard.nextInt();// first user imput asked to set up length of array
        System.out.println("What would you like to do next? Please pick an option from the following menu.");

        char grade[]= new char [id]; //declare array
        for (int j=0; j<=1; j++){ //beginning of menu loop
        {
           System.out.println("Menu");
    System.out.println("----");
      System.out.println("1) Print Gradebook");
        System.out.println("2) Update Grade");
          System.out.println("3) Exit");
          for (int i=0; i<=1; i++){ // beginning of grade input loop

          m = keyboard.nextInt();
          if (m == 2);{
            System.out.println("Enter student id to update:");
            sid = keyboard.nextInt();
            if (sid < 1);System.out.println("Invalid ID. Please try again.");
               i++;}
               if (sid >=1);{
                asid = sid - 1;

       System.out.println("Enter new grade for "+sid+":");
            char g = keyboard.nextChar(); //('cannot find symbol error keeps appearing...')
              grade[asid] = 'g'; //stores letter grade in appropriate place in Array
              System.out.print("New Grade for student # " + sid + "is " +g+ ".");
        {
                  while(sid <1 || sid > id);{
                      System.out.println("Invalid ID. Please try again."); //incorrect int throws error
                      j++;} // sends user back to menu to try again.
          }
           if (m > 3 || m < 1)
               System.out.println("Invalid choice. Please try again.");}
               j++;} //sends user back to menu to try again.


          if (m == 1);{ //variable m might not have been initialized

       for (y = 0;y<=grade.length;y++); //create output loop for student and grade printout
      int x= y + 1; //no one likes to be called student zero
      System.out.println("Student" + x + "has a current grade of " +grade[y]);{ //prints student and letter grade.
                              }

       if(m == 3);{ //(throws error 'variable m might not have been initialized') // if three is selected in main menu, program is exited.
               System.out.println("Exiting Gradebook...Goodbye!"); }
    }
        }}}}

There are several problems in this code, the if(m == 3);{ is, while not syntactically wrong, almost certainly will not do what you expect it to do. You should use remove the semicolon. At the moment if m equals three an empty statement will execute, and the code in the braces will always execute, regardless of the value of m.

The "variable m might not have been initialized" error is because you initialize m in the braces of the for loop and the if test(s) are outside the for loop, which means that if the for loop never executes (which doesn't appear possible, but the compiler doesn't know that) it will never be initialized. The solution to this could be to initialize m where you declare it, or move the if statements into the loop, which may be what you wanted in the first place.

There could be other things I haven't noticed but if you have further problems ask.

Thank you for your help. I finally got frustated enough that I started over, which led me to the realization that I was never going to force a char array into the keyboard programs that we are using in class. Once I went through my if statements and got the loops more defined, my issues went away. Yeah, I forgot to cut some extra variables from the program but it works, which is what I needed.

import java.io.*; 
 import java.util.*;

public class Gradelist{  
public static void main (String []args){
int i,j,m, sid, asid, y, id, counter; 
String g;
      SimpleInput keyboard = new SimpleInput ();       

        System.out.println("Greetings, welcome to the class grade tracking system. ");

        System.out.println("How many students are in the class?"); 
          id = keyboard.nextInt();

         if (id >0){     
             String grade[]= new String [id];
              for (int k=0; k<id;k++){

                  System.out.println("Enter student" +(k+1)+ "  grade.");
                  grade [k] = keyboard.nextLine ();      
             }
System.out.println("What would you like to do next? Please pick an option from the following menu.");
        for (j=0; j<=1;j++){

System.out.println("Menu");
    System.out.println("----");
      System.out.println("1) Print Gradebook");
        System.out.println("2) Update Grade");
          System.out.println("3) Exit");
              m = keyboard.nextInt();
              if (m == 2){
            System.out.println("Enter student id to update:");
            sid = keyboard.nextInt();

            if (sid > id) {
                System.out.println("Not possible, you only have " +id+ " students in this class.");  }
            else
              System.out.println(sid);
                  asid = sid -1;   

                       grade[asid] = keyboard.nextLine();

            System.out.println(grade[asid]);
            System.out.println("Student " + sid + " now has a current grade of " + grade[asid] + " .");
              }
              else if (m == 1) {

            for (int x =0; x <id; x++){
                 y= x+1;
                System.out.println("SID: " +y+" grade  " +grade[x]);}
              }
              else if (m == 3) {
               System.out.println("Exiting Gradebook...Goodbye!"); 
               System.exit(0);
           }
              else {
                  System.out.println("Invalid choice, please try again.");}
              j=0;
        }  }
      else {
          System.out.println("Your answer makes no sense,  you need to enter a postive number greater than zero. Please try again.");}}

      }        
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.