By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,882 Members | 745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,882 IT Pros & Developers. It's quick & easy.

Identifer expect error

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

Expand|Select|Wrap|Line Numbers
  1.  public class GradeCalculator extends JFrame 
  2.    { 
  3.       private static final int WIDTH = 550; 
  4.         private static final int HEIGHT = 430; 
  5.         int gradeCalc; 
  6.         int Student; 
  7.         private StudentList[] = new StudentList [20]; 
  8.         private static final int MAX_NUMBER_OF_STUDENTS = 20; 
  9.  
Jun 7 '10 #1
Share this Question
Share on Google+
11 Replies

jkmyoung
Expert 100+
P: 2,057
You haven't named your StudentList array. Really common mistake; I do it all the time.
Jun 7 '10 #2

P: 69
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.StudentList[s] = new Student();
^
GradeCalculator.java:143: cannot find symbol
symbol : class Student
location: class GradeCalculator
gradeCalc.StudentList[s] = new Student();
^
GradeCalculator.java:145: int cannot be dereferenced
gradeCalc.noOfStudents =
^
GradeCalculator.java:147: int cannot be dereferenced
gradeCalc.score =
^
GradeCalculator.java:150: int cannot be dereferenced
gradeCalc.getStudentData(inFile);
^
GradeCalculator.java:151: int cannot be dereferenced
gradeCalc.displayGradeAverage(0);
^
GradeCalculator.java:187: cannot find symbol
symbol : variable studentList
location: class GradeCalculator
boolean classAvg = studentList[(int) (totalScore / noOfStudents)].getClassAverage();
^
GradeCalculator.java:188: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
stNameTF.setText(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(""+StudentList[(int) (totalScore / 5.0)].getStudentAverage());
^
GradeCalculator.java:191: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
^
13 errors
Jun 7 '10 #3

jkmyoung
Expert 100+
P: 2,057
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];
Jun 7 '10 #4

P: 69
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
Jun 7 '10 #5

P: 69
this is my program:

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*;  
  2.    import java.io.*;
  3.    import javax.swing.*; 
  4.    import java.awt.*; 
  5.    import java.awt.event.*; 
  6.  
  7.     public class GradeCalculator extends JFrame 
  8.    { 
  9.       private static final int WIDTH = 550; 
  10.         private static final int HEIGHT = 430; 
  11.         int gradeCalc; 
  12.         int Student; 
  13.        private StudentList[] sList = new StudentList[20]; 
  14.         private static final int MAX_NUMBER_OF_STUDENTS = 20; 
  15.  
  16.                 // instance variables 
  17.       private int noOfStudents; 
  18.       private double score, tst1, tst2, tst3; 
  19.       private double  classAvg, stAvg, totalScore; 
  20.       private int displayedStudentIndex = 0; 
  21.       private char ltrGrade; 
  22.       private String stName; 
  23.  
  24.                 // This area is for the GUI components 
  25.                 // Each item that will be displayed will 
  26.                 // have a label and a textfield, (L) and (TF),  
  27.                 // respectively. 
  28.       private JLabel stNameL, tst1L, tst2L, tst3L, 
  29.                                         classAvgL, stAvgL, headingL; 
  30.       private JTextField stNameTF, tst1TF, tst2TF, tst3TF; 
  31.       private JTextArea classAvgTA, stAvgTA; 
  32.       private JButton exitB, nextB, prevB, calcGrade; 
  33.  
  34.       private ButtonHandler bHandler; 
  35.  
  36.        public GradeCalculator() 
  37.       { 
  38.          setTitle("Grade Calculator");           // set's the title 
  39.          setSize(WIDTH, HEIGHT);                 // set the window size 
  40.          Container pane = getContentPane();      // get the container 
  41.          pane.setLayout(null);                   // set the container's layout to null 
  42.  
  43.          bHandler = new ButtonHandler();         // instantiate the button event handler 
  44.  
  45.                 // instantiate the labels 
  46.          headingL = new JLabel("STUDENT RECORD"); 
  47.          stNameL = new JLabel("Student Name", SwingConstants.RIGHT); 
  48.          tst1L = new JLabel("Test 1", SwingConstants.LEFT); 
  49.          tst2L = new JLabel("Test 2", SwingConstants.LEFT); 
  50.          tst3L = new JLabel("Test 3", SwingConstants.LEFT); 
  51.          stAvgL = new JLabel("Student Average     " 
  52.                                                 + "\n"   + "Class Average"); 
  53.                 //instantiate the text fields 
  54.          stNameTF = new JTextField(65); 
  55.          tst1TF = new JTextField(10); 
  56.          tst2TF = new JTextField(10); 
  57.          tst3TF = new JTextField(10); 
  58.  
  59.                 // instantiate the text area 
  60.          classAvgTA = new JTextArea(6, 20); 
  61.          classAvgTA.setAutoscrolls(true); 
  62.  
  63.                 // instantiate the buttons and register the listener 
  64.          exitB = new JButton("Exit"); 
  65.          exitB.addActionListener(bHandler); 
  66.  
  67.          nextB = new JButton("Next"); 
  68.          nextB.addActionListener(bHandler); 
  69.  
  70.          prevB = new JButton("Previous"); 
  71.          prevB.addActionListener(bHandler); 
  72.  
  73.          calcGrade = new JButton("Calc Grade"); 
  74.          calcGrade.addActionListener(bHandler); 
  75.  
  76.                 // set the size of the labels, text fields, and buttons 
  77.  
  78.          headingL.setSize(200, 30); 
  79.          stNameL.setSize(100, 30); 
  80.          stNameTF.setSize(100, 30); 
  81.          tst1L.setSize(100, 30); 
  82.          tst1TF.setSize(100, 30); 
  83.          tst2L.setSize(120, 30); 
  84.          tst2TF.setSize(100, 30); 
  85.          tst3L.setSize(100, 30); 
  86.          tst3TF.setSize(100, 30); 
  87.          classAvgTA.setSize(370, 120); 
  88.          calcGrade.setSize(100, 30); 
  89.          prevB.setSize(100, 30);
  90.          nextB.setSize(100, 30); 
  91.          exitB.setSize(100, 30); 
  92.  
  93.                 //set the location of the labels, text fields, 
  94.                 //and buttons 
  95.          headingL.setLocation(220, 10); 
  96.          stNameL.setLocation(20, 50); 
  97.          stNameTF.setLocation(120, 50); 
  98.          tst1L.setLocation(20, 100); 
  99.          tst1TF.setLocation(120, 100); 
  100.          tst2L.setLocation(300, 50); 
  101.          tst2TF.setLocation(420, 50); 
  102.          tst3L.setLocation(300, 100); 
  103.          tst3TF.setLocation(420, 100); 
  104.          classAvgTA.setLocation(70, 230); 
  105.          prevB.setLocation(120, 370); 
  106.          exitB.setLocation(220, 370); 
  107.          nextB.setLocation(320, 370); 
  108.          calcGrade.setLocation(420, 370); 
  109.  
  110.                 //add the labels, text fields, and buttons to the pane 
  111.          pane.add(headingL); 
  112.          pane.add(stNameL); 
  113.          pane.add(stNameTF); 
  114.          pane.add(tst1L); 
  115.          pane.add(tst1TF); 
  116.          pane.add(tst2L); 
  117.          pane.add(tst2TF); 
  118.          pane.add(tst3L); 
  119.          pane.add(classAvgTA); 
  120.          pane.add(calcGrade); 
  121.          pane.add(prevB); 
  122.          pane.add(exitB); 
  123.          pane.add(nextB); 
  124.  
  125.          setVisible(true);         //show the window 
  126.          setDefaultCloseOperation(EXIT_ON_CLOSE); 
  127.          System.exit(0); 
  128.       } 
  129.  
  130.  
  131.  
  132.        public static void main (String [] args)
  133.       { 
  134.          new GradeCalculator(); 
  135.       } 
  136.  
  137.  
  138.  
  139.       Scanner inFile =  
  140.                                 new Scanner(new FileReader("AcademicGrades.txt")); 
  141.       {
  142.          for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++) 
  143.             gradeCalc.StudentList[s] = new Student(); 
  144.  
  145.          gradeCalc.noOfStudents = 
  146.                         inFile.nextInt();       // get the number of students 
  147.          gradeCalc.score = 
  148.                         inFile.nextDouble();    // get the student's scores 
  149.  
  150.          gradeCalc.getStudentData(inFile); 
  151.          gradeCalc.displayGradeAverage(0); 
  152.       }
  153.  
  154.                         // get the student data from file 
  155.  
  156.  
  157.        public void getStudentData(Scanner inFile) 
  158.       { 
  159.          System.out.println("Grade Calculator is getting information..."); 
  160.          System.out.println("One Moment Please"); 
  161.       } 
  162.        private class ButtonHandler implements ActionListener 
  163.       { 
  164.           public void actionPerformed (ActionEvent e) 
  165.          { 
  166.             if (e.getActionCommand().equals("Previous")) 
  167.                if (displayedStudentIndex > 0) 
  168.                   displayGradeAverage(displayedStudentIndex - 1); 
  169.                else 
  170.                   displayGradeAverage(displayedStudentIndex); 
  171.             else if (e.getActionCommand().equals("Next")) 
  172.                if (displayedStudentIndex + 1 < noOfStudents) 
  173.                   displayGradeAverage(displayedStudentIndex + 1); 
  174.                else 
  175.                   displayGradeAverage(displayedStudentIndex); 
  176.             else if (e.getActionCommand().equals("Calc Grade")) 
  177.                displayGradeAverage(0); 
  178.             else 
  179.                System.exit(0); 
  180.          } 
  181.       } 
  182.  
  183.        public void displayGradeAverage(int stName) 
  184.       { 
  185.          displayedStudentIndex = stName; 
  186.          String strName = ""; 
  187.          boolean classAvg = studentList[(int) (totalScore / noOfStudents)].getClassAverage(); 
  188.          stNameTF.setText(StudentList[stName].getFirstName() + " " 
  189.                                                             + studentList[stName].getLastName());
  190.          stAvgTA.setText(""+StudentList[(int) (totalScore / 5.0)].getStudentAverage());
  191.          classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
  192.    }
  193.  
Jun 7 '10 #6

jkmyoung
Expert 100+
P: 2,057
Where is your class for 1.?
Expand|Select|Wrap|Line Numbers
  1. 1. Need a class which will contain:
  2. a. Student Name
  3. b. Student Grades (an array of 3 grades)
  4. c. A constructor that clears the student data (use -1 for unset grades)
  5. d. Accessors (get functions) for each of the above, average, and letter grade
  6. e. Mutators (set functions) for items a, b, c
  7. f. Note that the accessor and mutator for Student grades has to have an argument for the grade index.
  8.  
Jun 7 '10 #7

P: 69
@jkmyoung
Oh wow, dont have it lol.
Jun 7 '10 #8

P: 69
ok i did that:

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*;  
  2.    import java.io.*;
  3.    import javax.swing.*; 
  4.    import java.awt.*; 
  5.    import java.awt.event.*; 
  6.  
  7.  
  8.     public class StudentList 
  9.      { 
  10.       String name;                          // Student's name. 
  11.         double test1, test2, test3;     // Grades on three tests. 
  12.         double getAverage() {              // compute average test grade 
  13.         return (test1 + test2 + test3) / 3; 
  14.       } 
  15.    }
  16.  
  17.  
  18.     public class GradeCalculator extends JFrame 
  19.    { 
  20.       private static final int WIDTH = 550; 
  21.       private static final int HEIGHT = 430; 
  22.       int gradeCalc; 
  23.       int student; 
  24.       private StudentList[] sList = new StudentList[20]; 
  25.  
  26.  
  27.                 // instance variables 
  28.       private int noOfStudents; 
  29.       private double score, tst1, tst2, tst3; 
  30.       private double  classAvg, stAvg, totalScore; 
  31.       private int displayedStudentIndex = 0; 
  32.       private char ltrGrade; 
  33.       private String stName; 
  34.  
  35.                 // This area is for the GUI components 
  36.                 // Each item that will be displayed will 
  37.                 // have a label and a textfield, (L) and (TF),  
  38.                 // respectively. 
  39.       private JLabel stNameL, tst1L, tst2L, tst3L, 
  40.                                         classAvgL, stAvgL, headingL; 
  41.       private JTextField stNameTF, tst1TF, tst2TF, tst3TF; 
  42.       private JTextArea classAvgTA, stAvgTA; 
  43.       private JButton exitB, nextB, prevB, calcGrade; 
  44.  
  45.       private ButtonHandler bHandler; 
  46.  
  47.        public GradeCalculator() 
  48.       { 
  49.          setTitle("Grade Calculator");           // set's the title 
  50.          setSize(WIDTH, HEIGHT);                 // set the window size 
  51.          Container pane = getContentPane();      // get the container 
  52.          pane.setLayout(null);                   // set the container's layout to null 
  53.  
  54.          bHandler = new ButtonHandler();         // instantiate the button event handler 
  55.  
  56.                 // instantiate the labels 
  57.          headingL = new JLabel("STUDENT RECORD"); 
  58.          stNameL = new JLabel("Student Name", SwingConstants.RIGHT); 
  59.          tst1L = new JLabel("Test 1", SwingConstants.LEFT); 
  60.          tst2L = new JLabel("Test 2", SwingConstants.LEFT); 
  61.          tst3L = new JLabel("Test 3", SwingConstants.LEFT); 
  62.          stAvgL = new JLabel("Student Average     " 
  63.                                                 + "\n"   + "Class Average"); 
  64.                 //instantiate the text fields 
  65.          stNameTF = new JTextField(65); 
  66.          tst1TF = new JTextField(10); 
  67.          tst2TF = new JTextField(10); 
  68.          tst3TF = new JTextField(10); 
  69.  
  70.                 // instantiate the text area 
  71.          classAvgTA = new JTextArea(6, 20); 
  72.          classAvgTA.setAutoscrolls(true); 
  73.  
  74.                 // instantiate the buttons and register the listener 
  75.          exitB = new JButton("Exit"); 
  76.          exitB.addActionListener(bHandler); 
  77.  
  78.          nextB = new JButton("Next"); 
  79.          nextB.addActionListener(bHandler); 
  80.  
  81.          prevB = new JButton("Previous"); 
  82.          prevB.addActionListener(bHandler); 
  83.  
  84.          calcGrade = new JButton("Calc Grade"); 
  85.          calcGrade.addActionListener(bHandler); 
  86.  
  87.                 // set the size of the labels, text fields, and buttons 
  88.  
  89.          headingL.setSize(200, 30); 
  90.          stNameL.setSize(100, 30); 
  91.          stNameTF.setSize(100, 30); 
  92.          tst1L.setSize(100, 30); 
  93.          tst1TF.setSize(100, 30); 
  94.          tst2L.setSize(120, 30); 
  95.          tst2TF.setSize(100, 30); 
  96.          tst3L.setSize(100, 30); 
  97.          tst3TF.setSize(100, 30); 
  98.          classAvgTA.setSize(370, 120); 
  99.          calcGrade.setSize(100, 30); 
  100.          prevB.setSize(100, 30);
  101.          nextB.setSize(100, 30); 
  102.          exitB.setSize(100, 30); 
  103.  
  104.                 //set the location of the labels, text fields, 
  105.                 //and buttons 
  106.          headingL.setLocation(220, 10); 
  107.          stNameL.setLocation(20, 50); 
  108.          stNameTF.setLocation(120, 50); 
  109.          tst1L.setLocation(20, 100); 
  110.          tst1TF.setLocation(120, 100); 
  111.          tst2L.setLocation(300, 50); 
  112.          tst2TF.setLocation(420, 50); 
  113.          tst3L.setLocation(300, 100); 
  114.          tst3TF.setLocation(420, 100); 
  115.          classAvgTA.setLocation(70, 230); 
  116.          prevB.setLocation(120, 370); 
  117.          exitB.setLocation(220, 370); 
  118.          nextB.setLocation(320, 370); 
  119.          calcGrade.setLocation(420, 370); 
  120.  
  121.                 //add the labels, text fields, and buttons to the pane 
  122.          pane.add(headingL); 
  123.          pane.add(stNameL); 
  124.          pane.add(stNameTF); 
  125.          pane.add(tst1L); 
  126.          pane.add(tst1TF); 
  127.          pane.add(tst2L); 
  128.          pane.add(tst2TF); 
  129.          pane.add(tst3L); 
  130.          pane.add(classAvgTA); 
  131.          pane.add(calcGrade); 
  132.          pane.add(prevB); 
  133.          pane.add(exitB); 
  134.          pane.add(nextB); 
  135.  
  136.          setVisible(true);         //show the window 
  137.          setDefaultCloseOperation(EXIT_ON_CLOSE); 
  138.          System.exit(0); 
  139.       } 
  140.  
  141.  
  142.        public static void main (String [] args)
  143.       { 
  144.          new GradeCalculator(); 
  145.       } 
  146.  
  147.       Scanner inFile =  
  148.                                 new Scanner(new FileReader("AcademicGrades.txt")); 
  149.       {
  150.          for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++) 
  151.             gradeCalc.StudentList[s] = new Student(); 
  152.  
  153.          gradeCalc.noOfStudents = 
  154.                         inFile.nextInt();       // get the number of Students 
  155.          gradeCalc.score = 
  156.                         inFile.nextDouble();    // get the Student's scores 
  157.  
  158.          gradeCalc.getStudentData(inFile); 
  159.          gradeCalc.displayGradeAverage(0); 
  160.       }
  161.  
  162.                         // get the Student data from file 
  163.  
  164.        public void getStudentData(Scanner inFile) 
  165.       { 
  166.          System.out.println("Grade Calculator is getting information..."); 
  167.          System.out.println("One Moment Please"); 
  168.       } 
  169.        private class ButtonHandler implements ActionListener 
  170.       { 
  171.           public void actionPerformed (ActionEvent e) 
  172.          { 
  173.             if (e.getActionCommand().equals("Previous")) 
  174.                if (displayedStudentIndex > 0) 
  175.                   displayGradeAverage(displayedStudentIndex - 1); 
  176.                else 
  177.                   displayGradeAverage(displayedStudentIndex); 
  178.             else if (e.getActionCommand().equals("Next")) 
  179.                if (displayedStudentIndex + 1 < noOfStudents) 
  180.                   displayGradeAverage(displayedStudentIndex + 1); 
  181.                else 
  182.                   displayGradeAverage(displayedStudentIndex); 
  183.             else if (e.getActionCommand().equals("Calc Grade")) 
  184.                displayGradeAverage(0); 
  185.             else 
  186.                System.exit(0); 
  187.          } 
  188.       } 
  189.  
  190.        public void displayGradeAverage(int stName) 
  191.       { 
  192.          displayedStudentIndex = stName; 
  193.          String strName = ""; 
  194.          boolean classAvg = StudentList[(int) (totalScore / noOfStudents)].getClassAverage(); 
  195.          stNameTF.setText(StudentList[stName].getFirstName() + " " 
  196.                                                             + StudentList[stName].getLastName());
  197.          stAvgTA.setText(""+StudentList[(int) (totalScore / 3.0)].getStudentAverage());
  198.          classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
  199.    }
  200.  
then i get these error i think its because it illegal but dont know how to fix it.

Errors:

StudentList.java:18: class GradeCalculator is public, should be declared in a file named GradeCalculator.java
public class GradeCalculator extends JFrame
^
StudentList.java:150: cannot find symbol
symbol : variable MAX_NUMBER_OF_STUDENTS
location: class GradeCalculator
for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++)
^
StudentList.java:151: int cannot be dereferenced
gradeCalc.StudentList[s] = new Student();
^
StudentList.java:151: cannot find symbol
symbol : class Student
location: class GradeCalculator
gradeCalc.StudentList[s] = new Student();
^
StudentList.java:153: int cannot be dereferenced
gradeCalc.noOfStudents =
^
StudentList.java:155: int cannot be dereferenced
gradeCalc.score =
^
StudentList.java:158: int cannot be dereferenced
gradeCalc.getStudentData(inFile);
^
StudentList.java:159: int cannot be dereferenced
gradeCalc.displayGradeAverage(0);
^
StudentList.java:194: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
boolean classAvg = StudentList[(int) (totalScore / noOfStudents)].getClassAverage();
^
StudentList.java:195: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
stNameTF.setText(StudentList[stName].getFirstName() + " "
^
StudentList.java:196: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
+ StudentList[stName].getLastName());
^
StudentList.java:197: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
stAvgTA.setText(""+StudentList[(int) (totalScore / 3.0)].getStudentAverage());
^
StudentList.java:198: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
^
13 errors
Jun 7 '10 #9

P: 69
I would like to know how can I use the stringValueOf to remove this error:

double cannot be dereferenced
gradeCalc.noOfStudents = inFile.nextInt(); // get the number of Students
^
edit reply report
Jun 8 '10 #10

jkmyoung
Expert 100+
P: 2,057
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.
Jun 8 '10 #11

Dheeraj Joshi
Expert 100+
P: 1,123
Are you using any IDE's? IDE's generally highlight the errors.

Regards
Dheeraj Joshi
Jun 9 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.