Identifer expect error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blknmld69
    New Member
    • Feb 2010
    • 69

    #1

    Identifer expect error

    I am having a problem getting my program to compile. I recieve the identifier error coming from my array.

    Code:
     public class GradeCalculator extends JFrame 
       { 
          private static final int WIDTH = 550; 
    		private static final int HEIGHT = 430; 
    		int gradeCalc; 
    		int Student; 
    		private StudentList[] = new StudentList [20]; 
    		private static final int MAX_NUMBER_OF_STUDENTS = 20;
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    You haven't named your StudentList array. Really common mistake; I do it all the time.

    Comment

    • blknmld69
      New Member
      • Feb 2010
      • 69

      #3
      when i name it:

      private StudentList[] sList= new StudentList [20];

      I get 13 errors instead of the one error i am having:

      GradeCalculator .java:13: cannot find symbol
      symbol : class StudentList
      location: class GradeCalculator
      private StudentList[] sList = new StudentList[20];
      ^
      GradeCalculator .java:13: cannot find symbol
      symbol : class StudentList
      location: class GradeCalculator
      private StudentList[] sList = new StudentList[20];
      ^
      GradeCalculator .java:143: int cannot be dereferenced
      gradeCalc.Stude ntList[s] = new Student();
      ^
      GradeCalculator .java:143: cannot find symbol
      symbol : class Student
      location: class GradeCalculator
      gradeCalc.Stude ntList[s] = new Student();
      ^
      GradeCalculator .java:145: int cannot be dereferenced
      gradeCalc.noOfS tudents =
      ^
      GradeCalculator .java:147: int cannot be dereferenced
      gradeCalc.score =
      ^
      GradeCalculator .java:150: int cannot be dereferenced
      gradeCalc.getSt udentData(inFil e);
      ^
      GradeCalculator .java:151: int cannot be dereferenced
      gradeCalc.displ ayGradeAverage( 0);
      ^
      GradeCalculator .java:187: cannot find symbol
      symbol : variable studentList
      location: class GradeCalculator
      boolean classAvg = studentList[(int) (totalScore / noOfStudents)].getClassAverag e();
      ^
      GradeCalculator .java:188: cannot find symbol
      symbol : variable StudentList
      location: class GradeCalculator
      stNameTF.setTex t(StudentList[stName].getFirstName() + " "
      ^
      GradeCalculator .java:189: cannot find symbol
      symbol : variable studentList
      location: class GradeCalculator
      + studentList[stName].getLastName()) ;
      ^
      GradeCalculator .java:190: cannot find symbol
      symbol : variable StudentList
      location: class GradeCalculator
      stAvgTA.setText (""+StudentL ist[(int) (totalScore / 5.0)].getStudentAver age());
      ^
      GradeCalculator .java:191: cannot find symbol
      symbol : variable StudentList
      location: class GradeCalculator
      classAvgTA.setT ext(""+StudentL ist[(int) (totalScore / noOfStudents)].getClassAverag e()); }
      ^
      13 errors

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        So this is supposed to be an integer array? eg
        private int StudentList [] = new int [20];
        or is it an array of students?
        private Student StudentList[] = new Student[20];

        Comment

        • blknmld69
          New Member
          • Feb 2010
          • 69

          #5
          the project just specifies:

          Create a program to enter grades and calculate averages and letter grades.
          1. Need a class which will contain:
          a. Student Name
          b. Student Grades (an array of 3 grades)
          c. A constructor that clears the student data (use -1 for unset grades)
          d. Accessors (get functions) for each of the above, average, and letter grade
          e. Mutators (set functions) for items a, b, c
          f. Note that the accessor and mutator for Student grades has to have an argument for the grade index.
          2. Need another class which will contain:
          a. An Array of Students (1 above)
          b. A count of number of students in use
          c. Constructor that reads data from a text file and sets up the students
          3. You need to create a graphical user interface that allows you to:
          a. Read data from file
          b. Add new students
          c. Process existing students
          d. Add test grades
          e. Based on a radio button setting display either the average or the letter grade
          f. Save modified data to file

          Comment

          • blknmld69
            New Member
            • Feb 2010
            • 69

            #6
            this is my program:

            Code:
             import java.util.*;  
               import java.io.*;
               import javax.swing.*; 
               import java.awt.*; 
               import java.awt.event.*; 
             
                public class GradeCalculator extends JFrame 
               { 
                  private static final int WIDTH = 550; 
            		private static final int HEIGHT = 430; 
            		int gradeCalc; 
            		int Student; 
            	   private StudentList[] sList = new StudentList[20]; 
            		private static final int MAX_NUMBER_OF_STUDENTS = 20; 
                     
                            // instance variables 
                  private int noOfStudents; 
                  private double score, tst1, tst2, tst3; 
                  private double  classAvg, stAvg, totalScore; 
                  private int displayedStudentIndex = 0; 
                  private char ltrGrade; 
                  private String stName; 
                     
                            // This area is for the GUI components 
                            // Each item that will be displayed will 
                            // have a label and a textfield, (L) and (TF),  
                            // respectively. 
                  private JLabel stNameL, tst1L, tst2L, tst3L, 
                                                    classAvgL, stAvgL, headingL; 
                  private JTextField stNameTF, tst1TF, tst2TF, tst3TF; 
                  private JTextArea classAvgTA, stAvgTA; 
                  private JButton exitB, nextB, prevB, calcGrade; 
                     
                  private ButtonHandler bHandler; 
                     
                   public GradeCalculator() 
                  { 
                     setTitle("Grade Calculator");           // set's the title 
                     setSize(WIDTH, HEIGHT);                 // set the window size 
                     Container pane = getContentPane();      // get the container 
                     pane.setLayout(null);                   // set the container's layout to null 
                                     
                     bHandler = new ButtonHandler();         // instantiate the button event handler 
                  
                            // instantiate the labels 
                     headingL = new JLabel("STUDENT RECORD"); 
                     stNameL = new JLabel("Student Name", SwingConstants.RIGHT); 
                     tst1L = new JLabel("Test 1", SwingConstants.LEFT); 
                     tst2L = new JLabel("Test 2", SwingConstants.LEFT); 
                     tst3L = new JLabel("Test 3", SwingConstants.LEFT); 
                     stAvgL = new JLabel("Student Average     " 
                                                            + "\n"   + "Class Average"); 
                            //instantiate the text fields 
                     stNameTF = new JTextField(65); 
                     tst1TF = new JTextField(10); 
                     tst2TF = new JTextField(10); 
                     tst3TF = new JTextField(10); 
                              
                            // instantiate the text area 
                     classAvgTA = new JTextArea(6, 20); 
                     classAvgTA.setAutoscrolls(true); 
                  
                            // instantiate the buttons and register the listener 
                     exitB = new JButton("Exit"); 
                     exitB.addActionListener(bHandler); 
                  
                     nextB = new JButton("Next"); 
                     nextB.addActionListener(bHandler); 
                  
                     prevB = new JButton("Previous"); 
                     prevB.addActionListener(bHandler); 
                  
                     calcGrade = new JButton("Calc Grade"); 
                     calcGrade.addActionListener(bHandler); 
                  
                            // set the size of the labels, text fields, and buttons 
                            
                     headingL.setSize(200, 30); 
                     stNameL.setSize(100, 30); 
                     stNameTF.setSize(100, 30); 
                     tst1L.setSize(100, 30); 
                     tst1TF.setSize(100, 30); 
                     tst2L.setSize(120, 30); 
                     tst2TF.setSize(100, 30); 
                     tst3L.setSize(100, 30); 
                     tst3TF.setSize(100, 30); 
                     classAvgTA.setSize(370, 120); 
                     calcGrade.setSize(100, 30); 
                     prevB.setSize(100, 30);
                     nextB.setSize(100, 30); 
                     exitB.setSize(100, 30); 
                               
                            //set the location of the labels, text fields, 
                            //and buttons 
                     headingL.setLocation(220, 10); 
                     stNameL.setLocation(20, 50); 
                     stNameTF.setLocation(120, 50); 
                     tst1L.setLocation(20, 100); 
                     tst1TF.setLocation(120, 100); 
                     tst2L.setLocation(300, 50); 
                     tst2TF.setLocation(420, 50); 
                     tst3L.setLocation(300, 100); 
                     tst3TF.setLocation(420, 100); 
                     classAvgTA.setLocation(70, 230); 
                     prevB.setLocation(120, 370); 
                     exitB.setLocation(220, 370); 
                     nextB.setLocation(320, 370); 
                     calcGrade.setLocation(420, 370); 
                  
                            //add the labels, text fields, and buttons to the pane 
                     pane.add(headingL); 
                     pane.add(stNameL); 
                     pane.add(stNameTF); 
                     pane.add(tst1L); 
                     pane.add(tst1TF); 
                     pane.add(tst2L); 
                     pane.add(tst2TF); 
                     pane.add(tst3L); 
                     pane.add(classAvgTA); 
                     pane.add(calcGrade); 
                     pane.add(prevB); 
                     pane.add(exitB); 
                     pane.add(nextB); 
                               
                     setVisible(true); 		//show the window 
                     setDefaultCloseOperation(EXIT_ON_CLOSE); 
                     System.exit(0); 
                  } 
                 	   
               
               
                   public static void main (String [] args)
                  { 
                     new GradeCalculator(); 
                  } 
               
                  
                     
                  Scanner inFile =  
                                            new Scanner(new FileReader("AcademicGrades.txt")); 
                  {
                     for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++) 
                        gradeCalc.StudentList[s] = new Student(); 
                     
                     gradeCalc.noOfStudents = 
                                    inFile.nextInt();       // get the number of students 
                     gradeCalc.score = 
                                    inFile.nextDouble();    // get the student's scores 
                     
                     gradeCalc.getStudentData(inFile); 
                     gradeCalc.displayGradeAverage(0); 
                  }
               
                                    // get the student data from file 
               							
               		
                   public void getStudentData(Scanner inFile) 
                  { 
                     System.out.println("Grade Calculator is getting information..."); 
                     System.out.println("One Moment Please"); 
                  } 
                   private class ButtonHandler implements ActionListener 
                  { 
                      public void actionPerformed (ActionEvent e) 
                     { 
                        if (e.getActionCommand().equals("Previous")) 
                           if (displayedStudentIndex > 0) 
                              displayGradeAverage(displayedStudentIndex - 1); 
                           else 
                              displayGradeAverage(displayedStudentIndex); 
                        else if (e.getActionCommand().equals("Next")) 
                           if (displayedStudentIndex + 1 < noOfStudents) 
                              displayGradeAverage(displayedStudentIndex + 1); 
                           else 
                              displayGradeAverage(displayedStudentIndex); 
                        else if (e.getActionCommand().equals("Calc Grade")) 
                           displayGradeAverage(0); 
                        else 
                           System.exit(0); 
                     } 
                  } 
               
                   public void displayGradeAverage(int stName) 
                  { 
                     displayedStudentIndex = stName; 
                     String strName = ""; 
                     boolean classAvg = studentList[(int) (totalScore / noOfStudents)].getClassAverage(); 
                     stNameTF.setText(StudentList[stName].getFirstName() + " " 
                        												+ studentList[stName].getLastName());
                     stAvgTA.setText(""+StudentList[(int) (totalScore / 5.0)].getStudentAverage());
                     classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
               }

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              Where is your class for 1.?
              Code:
              1. Need a class which will contain:
              a. Student Name
              b. Student Grades (an array of 3 grades)
              c. A constructor that clears the student data (use -1 for unset grades)
              d. Accessors (get functions) for each of the above, average, and letter grade
              e. Mutators (set functions) for items a, b, c
              f. Note that the accessor and mutator for Student grades has to have an argument for the grade index.

              Comment

              • blknmld69
                New Member
                • Feb 2010
                • 69

                #8
                Originally posted by jkmyoung
                Where is your class for 1.?
                Code:
                1. Need a class which will contain:
                a. Student Name
                b. Student Grades (an array of 3 grades)
                c. A constructor that clears the student data (use -1 for unset grades)
                d. Accessors (get functions) for each of the above, average, and letter grade
                e. Mutators (set functions) for items a, b, c
                f. Note that the accessor and mutator for Student grades has to have an argument for the grade index.
                Oh wow, dont have it lol.

                Comment

                • blknmld69
                  New Member
                  • Feb 2010
                  • 69

                  #9
                  ok i did that:

                  Code:
                   import java.util.*;  
                     import java.io.*;
                     import javax.swing.*; 
                     import java.awt.*; 
                     import java.awt.event.*; 
                  	
                  	
                      public class StudentList 
                  	 { 
                        String name; 					 	// Student's name. 
                  		double test1, test2, test3; 	// Grades on three tests. 
                  		double getAverage() { 		 	// compute average test grade 
                  		return (test1 + test2 + test3) / 3; 
                        } 
                     }
                  
                   
                      public class GradeCalculator extends JFrame 
                     { 
                        private static final int WIDTH = 550; 
                        private static final int HEIGHT = 430; 
                        int gradeCalc; 
                        int student; 
                        private StudentList[] sList = new StudentList[20]; 
                     	 
                           
                                  // instance variables 
                        private int noOfStudents; 
                        private double score, tst1, tst2, tst3; 
                        private double  classAvg, stAvg, totalScore; 
                        private int displayedStudentIndex = 0; 
                        private char ltrGrade; 
                        private String stName; 
                           
                                  // This area is for the GUI components 
                                  // Each item that will be displayed will 
                                  // have a label and a textfield, (L) and (TF),  
                                  // respectively. 
                        private JLabel stNameL, tst1L, tst2L, tst3L, 
                                                          classAvgL, stAvgL, headingL; 
                        private JTextField stNameTF, tst1TF, tst2TF, tst3TF; 
                        private JTextArea classAvgTA, stAvgTA; 
                        private JButton exitB, nextB, prevB, calcGrade; 
                           
                        private ButtonHandler bHandler; 
                           
                         public GradeCalculator() 
                        { 
                           setTitle("Grade Calculator");           // set's the title 
                           setSize(WIDTH, HEIGHT);                 // set the window size 
                           Container pane = getContentPane();      // get the container 
                           pane.setLayout(null);                   // set the container's layout to null 
                                           
                           bHandler = new ButtonHandler();         // instantiate the button event handler 
                        
                                  // instantiate the labels 
                           headingL = new JLabel("STUDENT RECORD"); 
                           stNameL = new JLabel("Student Name", SwingConstants.RIGHT); 
                           tst1L = new JLabel("Test 1", SwingConstants.LEFT); 
                           tst2L = new JLabel("Test 2", SwingConstants.LEFT); 
                           tst3L = new JLabel("Test 3", SwingConstants.LEFT); 
                           stAvgL = new JLabel("Student Average     " 
                                                                  + "\n"   + "Class Average"); 
                                  //instantiate the text fields 
                           stNameTF = new JTextField(65); 
                           tst1TF = new JTextField(10); 
                           tst2TF = new JTextField(10); 
                           tst3TF = new JTextField(10); 
                                    
                                  // instantiate the text area 
                           classAvgTA = new JTextArea(6, 20); 
                           classAvgTA.setAutoscrolls(true); 
                        
                                  // instantiate the buttons and register the listener 
                           exitB = new JButton("Exit"); 
                           exitB.addActionListener(bHandler); 
                        
                           nextB = new JButton("Next"); 
                           nextB.addActionListener(bHandler); 
                        
                           prevB = new JButton("Previous"); 
                           prevB.addActionListener(bHandler); 
                        
                           calcGrade = new JButton("Calc Grade"); 
                           calcGrade.addActionListener(bHandler); 
                        
                                  // set the size of the labels, text fields, and buttons 
                                  
                           headingL.setSize(200, 30); 
                           stNameL.setSize(100, 30); 
                           stNameTF.setSize(100, 30); 
                           tst1L.setSize(100, 30); 
                           tst1TF.setSize(100, 30); 
                           tst2L.setSize(120, 30); 
                           tst2TF.setSize(100, 30); 
                           tst3L.setSize(100, 30); 
                           tst3TF.setSize(100, 30); 
                           classAvgTA.setSize(370, 120); 
                           calcGrade.setSize(100, 30); 
                           prevB.setSize(100, 30);
                           nextB.setSize(100, 30); 
                           exitB.setSize(100, 30); 
                                     
                                  //set the location of the labels, text fields, 
                                  //and buttons 
                           headingL.setLocation(220, 10); 
                           stNameL.setLocation(20, 50); 
                           stNameTF.setLocation(120, 50); 
                           tst1L.setLocation(20, 100); 
                           tst1TF.setLocation(120, 100); 
                           tst2L.setLocation(300, 50); 
                           tst2TF.setLocation(420, 50); 
                           tst3L.setLocation(300, 100); 
                           tst3TF.setLocation(420, 100); 
                           classAvgTA.setLocation(70, 230); 
                           prevB.setLocation(120, 370); 
                           exitB.setLocation(220, 370); 
                           nextB.setLocation(320, 370); 
                           calcGrade.setLocation(420, 370); 
                        
                                  //add the labels, text fields, and buttons to the pane 
                           pane.add(headingL); 
                           pane.add(stNameL); 
                           pane.add(stNameTF); 
                           pane.add(tst1L); 
                           pane.add(tst1TF); 
                           pane.add(tst2L); 
                           pane.add(tst2TF); 
                           pane.add(tst3L); 
                           pane.add(classAvgTA); 
                           pane.add(calcGrade); 
                           pane.add(prevB); 
                           pane.add(exitB); 
                           pane.add(nextB); 
                                     
                           setVisible(true); 		//show the window 
                           setDefaultCloseOperation(EXIT_ON_CLOSE); 
                           System.exit(0); 
                        } 
                       	      
                     
                         public static void main (String [] args)
                        { 
                           new GradeCalculator(); 
                        } 
                                    
                        Scanner inFile =  
                                                  new Scanner(new FileReader("AcademicGrades.txt")); 
                        {
                           for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++) 
                              gradeCalc.StudentList[s] = new Student(); 
                           
                           gradeCalc.noOfStudents = 
                                          inFile.nextInt();       // get the number of Students 
                           gradeCalc.score = 
                                          inFile.nextDouble();    // get the Student's scores 
                           
                           gradeCalc.getStudentData(inFile); 
                           gradeCalc.displayGradeAverage(0); 
                        }
                     
                                          // get the Student data from file 
                     						   		
                         public void getStudentData(Scanner inFile) 
                        { 
                           System.out.println("Grade Calculator is getting information..."); 
                           System.out.println("One Moment Please"); 
                        } 
                         private class ButtonHandler implements ActionListener 
                        { 
                            public void actionPerformed (ActionEvent e) 
                           { 
                              if (e.getActionCommand().equals("Previous")) 
                                 if (displayedStudentIndex > 0) 
                                    displayGradeAverage(displayedStudentIndex - 1); 
                                 else 
                                    displayGradeAverage(displayedStudentIndex); 
                              else if (e.getActionCommand().equals("Next")) 
                                 if (displayedStudentIndex + 1 < noOfStudents) 
                                    displayGradeAverage(displayedStudentIndex + 1); 
                                 else 
                                    displayGradeAverage(displayedStudentIndex); 
                              else if (e.getActionCommand().equals("Calc Grade")) 
                                 displayGradeAverage(0); 
                              else 
                                 System.exit(0); 
                           } 
                        } 
                           
                         public void displayGradeAverage(int stName) 
                        { 
                           displayedStudentIndex = stName; 
                           String strName = ""; 
                           boolean classAvg = StudentList[(int) (totalScore / noOfStudents)].getClassAverage(); 
                           stNameTF.setText(StudentList[stName].getFirstName() + " " 
                              												+ StudentList[stName].getLastName());
                           stAvgTA.setText(""+StudentList[(int) (totalScore / 3.0)].getStudentAverage());
                           classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
                     }
                  then i get these error i think its because it illegal but dont know how to fix it.

                  Errors:

                  StudentList.jav a:18: class GradeCalculator is public, should be declared in a file named GradeCalculator .java
                  public class GradeCalculator extends JFrame
                  ^
                  StudentList.jav a:150: cannot find symbol
                  symbol : variable MAX_NUMBER_OF_S TUDENTS
                  location: class GradeCalculator
                  for (int s = 0; s < MAX_NUMBER_OF_S TUDENTS; s++)
                  ^
                  StudentList.jav a:151: int cannot be dereferenced
                  gradeCalc.Stude ntList[s] = new Student();
                  ^
                  StudentList.jav a:151: cannot find symbol
                  symbol : class Student
                  location: class GradeCalculator
                  gradeCalc.Stude ntList[s] = new Student();
                  ^
                  StudentList.jav a:153: int cannot be dereferenced
                  gradeCalc.noOfS tudents =
                  ^
                  StudentList.jav a:155: int cannot be dereferenced
                  gradeCalc.score =
                  ^
                  StudentList.jav a:158: int cannot be dereferenced
                  gradeCalc.getSt udentData(inFil e);
                  ^
                  StudentList.jav a:159: int cannot be dereferenced
                  gradeCalc.displ ayGradeAverage( 0);
                  ^
                  StudentList.jav a:194: cannot find symbol
                  symbol : variable StudentList
                  location: class GradeCalculator
                  boolean classAvg = StudentList[(int) (totalScore / noOfStudents)].getClassAverag e();
                  ^
                  StudentList.jav a:195: cannot find symbol
                  symbol : variable StudentList
                  location: class GradeCalculator
                  stNameTF.setTex t(StudentList[stName].getFirstName() + " "
                  ^
                  StudentList.jav a:196: cannot find symbol
                  symbol : variable StudentList
                  location: class GradeCalculator
                  + StudentList[stName].getLastName()) ;
                  ^
                  StudentList.jav a:197: cannot find symbol
                  symbol : variable StudentList
                  location: class GradeCalculator
                  stAvgTA.setText (""+StudentL ist[(int) (totalScore / 3.0)].getStudentAver age());
                  ^
                  StudentList.jav a:198: cannot find symbol
                  symbol : variable StudentList
                  location: class GradeCalculator
                  classAvgTA.setT ext(""+StudentL ist[(int) (totalScore / noOfStudents)].getClassAverag e()); }
                  ^
                  13 errors

                  Comment

                  • blknmld69
                    New Member
                    • Feb 2010
                    • 69

                    #10
                    I would like to know how can I use the stringValueOf to remove this error:

                    double cannot be dereferenced
                    gradeCalc.noOfS tudents = inFile.nextInt( ); // get the number of Students
                    ^
                    edit reply report

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      What is the variable gradeCalc supposed to represent?
                      Shouldn't it just be
                      noOfStudents = inFile.nextInt( );
                      ?


                      It looks like you're treating gradeCalc as a GradeCalculator object, when you've declared it as a non-static int.

                      Comment

                      • Dheeraj Joshi
                        Recognized Expert Top Contributor
                        • Jul 2009
                        • 1129

                        #12
                        Are you using any IDE's? IDE's generally highlight the errors.

                        Regards
                        Dheeraj Joshi

                        Comment

                        Working...