9 Replies Latest reply: Jun 13, 2007 4:18 PM by 807600 RSS

    identifier expected error with custom class component in arraylist

    807600
      Hi, this is my 7th day of learning java, so please bear with me if I use the wrong names, etc

      I need to make my own type for what I want to do.
      Then I want to make an ArrayList of it (because I don't know the size).
      I need my new type to contain 5 variables (or is it fields?, wait, or is it elements? sigh) and have the user only give the first value for each element in the array, which is a string. the other fields must be automatically (initialized?) to the values I want.

      1- am i declaring those default values at the right place?
      1a - is the void in the constructer correct programming?
      2- this is what is my problem. why do i get the <identifier expected> error? what's wrong with my argument? it is a string, so i'm stumped here.
          public class MyEieTipe {
              //the MyEieTipe class has 5 variables currently
              public String dieNaam;
              public float dieTelling;
              public int dieVegte;
              public boolean dieHide;
              public boolean dieBeskerm;
              //the MyEieTipe class has 1 constructor
              public void MyEieTipe(String startNaam) {
                  dieNaam = startNaam;
                  dieTelling = 0;
                  dieVegte = 0;
                  dieHide = false;
                  dieBeskerm = false;
              }
          }
        
          
          ArrayList<MyEieTipe> myTipe = new ArrayList();
          myTipe.add("hello");    //gives <identifier expected> error
      Any help greatly appreciated!!
        • 1. Re: identifier expected error with custom class component in arraylist
          807600
          You have to first create an object of MyEieTipe before you can add it to the the ArrayList.

          So your code will look like this:
          MyEieTipe obj = new MyEieTipe("hello");
          myTipe.add(obj);
          • 2. Re: identifier expected error with custom class component in arraylist
            807600
            Thanks! It still gives the error though...
                ArrayList<MyEieTipe> myTipe = new ArrayList();
                MyEieTipe obj = new MyEieTipe("hello");
                myTipe.add(obj);        //still gives <identifier expected> error
            PS: i'm first language isn't English, so that class name is actually very descriptive in my language, unless you meant something else?
            • 3. Re: identifier expected error with custom class component in arraylist
              807600
              ad 1) Yes - constructor is the right place to initialize properties (data variables in a object).
              ad 1a) Sorry - you have to declare it without type! If you include any type (like void in this case) you didn't declare constructor but normal method (by coincidence using same name as class). You must declare constructor like this:
              public MyEieTipe(String startNaam)
              ad 2) I think you want to add new object of the class MyEieTipe, right? So you have to make it:
              myTipe.add (new("hello") ); 
              • 4. Re: identifier expected error with custom class component in arraylist
                807600
                Yes, of course I forget name of new class ;)

                myTipe.add (new MyEieTipe("hello") );
                • 5. Re: identifier expected error with custom class component in arraylist
                  807600
                  Thanks! It still gives the error though...
                      ArrayList<MyEieTipe> myTipe = new
                  ArrayList();
                  MyEieTipe obj = new MyEieTipe("hello");
                  myTipe.add(obj);        //still gives <identifier
                  expected> error
                  PS: i'm first language isn't English, so that
                  class name is actually very descriptive in my
                  language, unless you meant something else?
                  Delete the void identifier from your constructor and the code should work. By adding void you are declaring a normal function which has the same name as your constructor. When you call new to create a new object of your class, the constructor that you are trying to call is not invoked because the java compiler considers it a normal method.

                  Hope this helps.
                  • 6. Re: identifier expected error with custom class component in arraylist
                    807600
                    Thanks! That also doesn't seem to work....

                        ArrayList<MyEieTipe> myTipe = new ArrayList();
                        myTipe.add(new MyEieTipe("Hello"));        //also still gives <identifier expected> error
                    oh boy......

                    PS: also tried it with taking out the void. also didn't work. gonna leave it as a constructor (without the void) from now on.
                    • 7. Re: identifier expected error with custom class component in arraylist
                      807600
                      Here is the working code:
                      import java.util.*;
                      public class test {
                      
                           /**
                            * @param args
                            */
                           public static void main(String[] args) {
                                // TODO Auto-generated method stub
                                ArrayList<MyEieTipe> myTipe = new ArrayList();
                                MyEieTipe obj = new MyEieTipe("hello");
                                myTipe.add(obj);
                           }
                      
                      }
                      
                      
                      class MyEieTipe {
                          //the MyEieTipe class has 5 variables currently
                          public String dieNaam;
                          public float dieTelling;
                          public int dieVegte;
                          public boolean dieHide;
                          public boolean dieBeskerm;
                          //the MyEieTipe class has 1 constructor
                          public MyEieTipe(String startNaam) {
                              dieNaam = startNaam;
                              dieTelling = 0;
                              dieVegte = 0;
                              dieHide = false;
                              dieBeskerm = false;
                          }
                      }
                      • 8. Re: identifier expected error with custom class component in arraylist
                        807600
                        Thanks! this is green for go!!
                                public MyEieTipe(String startNaam) {
                                    dieNaam = startNaam;
                                    dieTelling = 0;
                                    dieVegte = 0;
                                    dieHide = false;
                                    dieBeskerm = false;
                                }
                            }
                        
                            
                            ArrayList<MyEieTipe> myTipe = new ArrayList();
                            MyEieTipe obj = new MyEieTipe("hello");
                        • 9. Re: identifier expected error with custom class component in arraylist
                          807600
                          I also noticed that when you are declaring Generics, they need to be declared on both sides of the equality for generics to work properly:
                          ArrayList<MyEieTipe> myTipe = new ArrayList(); //wrong
                          
                          ArrayList<MyEieTipe> myTipe = new ArrayList<MyEieTipe>(); // right