1 2 Previous Next 29 Replies Latest reply on May 7, 2008 2:39 PM by 807601

    Method undefined in ArrayList<Student> error, can anyone help?

    807601
      The code below is supposed to add a student to the enrolledStudents
      list after confirming they have registered (Registration occurs in Student
      class)

      My Student class contains the master list (studentRegistry), of all students
      who have ever studied at the Training Center.

      My Course class (of which this snippet is a part) attempts to verify a Student
      has registered before enrolling them in their selected Course.

      Each Course maintains a (sub)List of enrolled Students which contains only the
      Students name and the reference (index) to their corresponding record in the
      studentRegistry.

      If the Student isn't yet registered, we will register them first (but since the
      lookup is broken, I haven't bothered with this yet)

      standardCharge is one of the Course fields, other fields (within the braces)
      are derived

      studentName is the unique identifier for Students
           public void enrolStudent(int index, Student studentName) {
                double standardCharge, rebate, priceRate, nettCharge;
                int studentReference;
      
                if (students.isEmpty())
                     index = 0;
                else
                     index = students.size();
                // lookup the Student
                if (studentRegistry.contains(studentName))
                     studentReference = studentRegistry.indexOf(studentName);
                else
                     studentRegistry.add(new Student(studentName));
                // TODO   Call applicable discounts and apply them
                      //             this will provide the values for the fields below
                nettCharge = (standardCharge - rebate) * priceRate;
                      // TODO   Send the nettCharge back to Student.setCourseFees(nettCharge)
           }
      I keep getting an error that studentRegistry cannot be resolved.

      Is this because I am trying to access the variable directly rather than creating something like:

      In Student class
      public int lookupStudent(String studentName){
          if (studentRegistry.contains(studentName))  {
           studentRef = studentRegistry.indexOf(studentName);
          } else {
           studentRegistry.add(new Student(studentName));
           studentRef = studentRegistry.indexOf(studentName);
          }        
          return studentRef;
      }
      (I think) I should then be able to utilize studentRef to access the other fields I need.
        • 1. Re: Method undefined in ArrayList<Student> error, can anyone help?
          807601
          you need to define studentregistry as a member or static variable in your class
          • 2. Re: Method undefined in ArrayList<Student> error, can anyone help?
            807601
            I gathered as much ... I replaced my original code with my proposed fix
            and now I'm getting "Cannot make a static reference to the non-static method
            lookupStudent(String) from the type Student"

            If I make the method static ... everything inside it turns to red squiggles.

            Should I have written
            public ArrayList<Student> studentRegistry = new ArrayList<Student>();
            as
            public static ArrayList<Student> studentRegistry = new ArrayList<Student>();
            I realise this is (fairly) basic stuff, it's frustrating to me that I'm missing it.

            How do I setup the list(s) so that other classes can read from them?
            • 3. Re: Method undefined in ArrayList<Student> error, can anyone help?
              807601
              kcstingel wrote:
              If I make the method static ... everything inside it turns to red squiggles.
              Don't do that.
              Should I have written
              public ArrayList<Student> studentRegistry = new ArrayList<Student>();
              as
              public static ArrayList<Student> studentRegistry = new ArrayList<Student>();
              No, don't do that.

              You say that MyStudent contains the studentRegistry list, and I'm guessing that the MyClass object has a reference to the MyStudent object. Then perhaps you need a public method within MyStudent that is able to test to see if the student is within the registry list or not that MyClass can call, something like
              public boolean inRegistry(Student student)
              {
                 // TODO fill in code
              } 
              If this doesn't help, then you might have better luck getting a helpful response if you create a Short, Self Contained, Correct (Compilable), Example or SSCCE. This is a small application that you create that is compilable and runnable, and demonstrates your error, but contains no extraneous, unnecessary code that is not associated with your problem. To see more about this and how to create this, please look at this link:

              [http://homepage1.nifty.com/algafield/sscce.html|http://homepage1.nifty.com/algafield/sscce.html]

              Remember, if this code is compilable and runnable more here will be able to help you.

              Good luck.
              • 4. Re: Method undefined in ArrayList<Student> error, can anyone help?
                3004
                What are the current errors? Paste in the exact, complete error message, and indicate clearly which line it's coming from.
                • 5. Re: Method undefined in ArrayList<Student> error, can anyone help?
                  807601
                  Nitpick:
                  public void enrolStudent(int index, Student studentName) {
                      if(students.isEmpty())
                          index = 0;
                      else
                          index = students.size();
                  Why bother passing a value to the index parameter if you immediately overwrite it?
                  • 6. Re: Method undefined in ArrayList<Student> error, can anyone help?
                    807601
                    Apologies, I had mentioned the errors earlier ...
                    public void setDiscounts() {
                         String name;
                      /** Cannot make a static reference to the non-static method 
                       * lookupStudent(String) from the type TestSystem2.Student */
                         int studentRef = Student.lookupStudent(name);
                         // TODO Need to fix 
                         /** studentRegistry cannot be resolved */
                         if (studentRegistry.get(studentRef).isFifty())
                              rebate = 100.0;
                         else
                              rebate = 0.0;
                         /** studentRegistry cannot be resolved */
                         if (studentRegistry.get(studentRef).isReturning())
                              priceRate = 0.8; // 20% off
                         else
                              rebate = 1.0;
                    }
                    
                    /** Cannot make a static reference to the non-static 
                     *  field Student.studentRegistry */
                    Student.studentRegistry.add(new Student("Im Confused",
                    
                    
                    /** Cannot make a static reference to the non-static 
                     *  field Course.courselist */
                    Course.courselist.add(new Course(1, "Italian Cooking",
                              "Ann Instructor", 10, 500.00, 1000.00 
                              + (enrolledStudents * 50.00)));
                    I omitted the other errors because they are just repeats of those mentioned here.
                    • 7. Re: Method undefined in ArrayList<Student> error, can anyone help?
                      3004
                      flounder wrote:
                      Nitpick:
                      public void enrolStudent(int index, Student studentName) {
                      if(students.isEmpty())
                      index = 0;
                      else
                      index = students.size();
                      Why bother passing a value to the index parameter if you immediately overwrite it?
                      I wouldn't call that a nitpick. At best it might be a small oversight/typo on the OP's part, but there's a good chance it's a sign of a deeper misunderstanding.
                      • 8. Re: Method undefined in ArrayList<Student> error, can anyone help?
                        3004
                        kcstingel wrote:
                        /** Cannot make a static reference to the non-static method
                        [http://www.google.com/search?%22static+context%22+java]
                        public class Foo {
                          public static void staticBar() {}
                        
                          public void nonStaticBaz() {}
                        }
                        
                        public class MainClass {
                          public static void staticQux() {}
                        
                          public void nonStaticSplort() {}
                        
                          public static void main(String[] args) {
                            Foo.staticBar(); // OK
                            staticQux(); // OK
                        
                            Foo.nonStaticBaz(); // ERROR
                            nonStaticSplort(); // ERROR
                        
                            Foo foo = new Foo();
                            MainClass mainObject = new MainClass();
                        
                            foo.nonStaticBaz(); // OK
                            mainObject.nonStaticSplort(); // OK
                          }
                        }
                        • 9. Re: Method undefined in ArrayList<Student> error, can anyone help?
                          807601
                          flounder wrote:
                          Nitpick:
                          public void enrolStudent(int index, Student studentName) {
                          if(students.isEmpty())
                          index = 0;
                          else
                          index = students.size();
                          Why bother passing a value to the index parameter if you immediately overwrite it?
                          because I'm setting the value of index, so I can then use
                          courselist.add(index, studentName, studentRef);
                          to add the student (once registration is confirmed) to the Course.
                          • 10. Re: Method undefined in ArrayList<Student> error, can anyone help?
                            807601
                            kcstingel wrote:
                            because I'm setting the value of index, so I can then use
                            courselist.add(index, studentName, studentRef);
                            to add the student (once registration is confirmed) to the Course.
                            But don't you see how it farks up your using index as a loop index?? It completely borks the loop (bork -- I love that word). Because of this a basic rule of programming that is rarely broken is: thou shalt not change a loop index variable within the loop.
                            • 11. Re: Method undefined in ArrayList<Student> error, can anyone help?
                              807601
                              Hi, everyone
                              According to reply 9, the following code should work

                              in the Test class, there is such a method
                              // This method loads a set of questions from a plain text file
                                  public void loadQuestionsFromFile()throws FileNotFoundException
                                  {
                                      String questionType = "";
                                      int pointsValue = 0;
                                      String category = "";
                                      int difficultyLevel = 0;
                                      String questionText = "";
                                      String correctAnswer = "";
                                      String answerChoices = "";
                                      String questionTerminator = "";
                                          
                                      
                                      File fileReader = new File("testquestions.txt");
                                      Scanner sc = new Scanner(fileReader);
                                      
                                      if (sc == null)
                                      {
                                          throw new FileNotFoundException("File cannot be found");
                                      }
                                          
                                          
                                          while (sc.hasNextLine())
                                          {
                                              String temp = sc.nextLine();
                                              if (temp.equals("TF"))
                                              {
                                                  questionType = sc.nextLine();
                                                  pointsValue = Integer.parseInt(sc.nextLine());
                                                  category = sc.nextLine();
                                                  difficultyLevel = Integer.parseInt(sc.nextLine());
                                                  questionText = sc.nextLine();
                                                  correctAnswer = sc.nextLine();
                                                  questionTerminator = sc.nextLine();
                                              }
                                              
                                              else if (temp.equals("MC"))
                                              {
                                                  questionType = sc.nextLine();
                                                  pointsValue = Integer.parseInt(sc.nextLine());
                                                  category = sc.nextLine();
                                                  difficultyLevel = Integer.parseInt(sc.nextLine());
                                                  questionText = sc.nextLine();
                                                  correctAnswer = sc.nextLine();
                                                  answerChoices = sc.nextLine() + sc.nextLine() + sc. nextLine() + sc.nextLine();
                                                  questionTerminator = sc.nextLine();
                                              }
                                                  
                                              Question x = new Question(questionText, correctAnswer, pointsValue, category, difficultyLevel);
                                              addQuestion(x);
                                              sc.nextLine();
                                          }
                                          
                                          //fileReader.close();
                                          
                                      }   
                              And in the main class--QuizDriver class
                              I try
                              try
                                      {
                                          Test test = new Test();
                                          test.loadQuestionsFromFile();
                                      }
                                      catch (FileNotFoundException a)
                                      {
                                          System.err.println(a);
                                      }
                              When I compile the main class, the compiler throws me back an error message exactly as follow
                              cannot find symbol - constructor Test();
                              one the following line of code
                              Test test = new Test();
                              What is the problem here?
                              I also have a Test constructor in the Test class, would this be the problem?
                              Thank you

                              Edited by: Terry001 on May 5, 2008 8:03 PM
                              • 12. Re: Method undefined in ArrayList<Student> error, can anyone help?
                                3004
                                kcstingel wrote:
                                flounder wrote:
                                Nitpick:
                                public void enrolStudent(int index, Student studentName) {
                                if(students.isEmpty())
                                index = 0;
                                else
                                index = students.size();
                                Why bother passing a value to the index parameter if you immediately overwrite it?
                                because I'm setting the value of index, so I can then use
                                courselist.add(index, studentName, studentRef);
                                to add the student (once registration is confirmed) to the Course.
                                No.
                                void foo(int i) {
                                  i = 1;
                                  System.out.println(i);
                                }
                                Why have the parameter "i"? Parameters are for callers to pass information to a method. Calling foo(12345) with the above is exactly the same as calling foo(-9876), which is exactly the same as calling foo() below.
                                void foo() {
                                  int i = 1;
                                  System.out.println(i);
                                }
                                So what is the point of having the "i" parameter?
                                • 13. Re: Method undefined in ArrayList<Student> error, can anyone help?
                                  3004
                                  Terry001 wrote:
                                  Hi, everyone
                                  According to reply 9, the following code should work
                                  Reply 9 is 100% correct, so at least one of the follwing is true:

                                  1) The code you supplied does work.

                                  2) Reply 9 does not state, claim, suggest, or imply that the code you supplied should work.
                                  When I compile the main class, the compiler throws me back an error message exactly as follow
                                  cannot find symbol - constructor Test();
                                  Ah, it's #2.

                                  Reply 9 most certainly does not state, claim, suggest, or imply that you should be able to invoke a constructor that does not exist.

                                  See my explanation in your other thread where you posted this issue.
                                  • 14. Re: Method undefined in ArrayList<Student> error, can anyone help?
                                    807601
                                    For Clarification:

                                    class Student has a List (registry)
                                    Course list maintains a List (courselist) with a Sublist (students)

                                    class Course needs to call (registry) to confirm Student exists
                                    upon confirmation, we need to
                                    setRef(registry.indexOf(name));
                                    to learn the value to record for ref.
                                    Course will add Student to it's own Sublist (students)

                                    The Sublist (students) will store 2 values: (name, ref)

                                    I need help with:

                                    an Accessor for registry, so that Course can read the list
                                    a Setter for Course to send pricing data back to registry
                                    setPrice(registry.addFees(ref, price))
                                    1 2 Previous Next