0

I made a class that returns 3 values of an arraylist to the main program but when I use my get() method the compiler throws me en error that cant find this method - symbol. I tried the same thing doing it with not an arraylist but with an array and it seems to work. But I cant figure the way it will be with an Array List.

//Error is on "action evt" show button on second mark of code.

Error msg is: "Cannot find symbol-method getmarka()".

I am posting the code below maybe you can help me.

The Class with get method that returns the values:

    public class Cars
{
    private String modelo,marka;
    private int kyvismos;
    public Cars(String m,String ma,int k)
    {
       modelo=m;
       marka=ma;
       kyvismos=k;
    }
    public String getmodelo()
    {
        return modelo;
    }
    public String getmarka()
    {
        return marka;
    }
    public int getkyvismos()
    {
        return kyvismos;
    }
    public void setmodelo(String m)
    {
        modelo=m;
    }
    public void setmarka(String ma)
    {
        marka=ma;
    }
    public void setkyvismos(int k)
    {
        kyvismos=k;
    }
    public String toString()
    {
        return modelo+","+marka+","+kyvismos;
    }


}

Here is the Class that gets the category-class on top.

    public class MyFrame extends Frame
{
    ArrayList<Cars> Cars = new ArrayList<>();
    private Button add;
    private Button show;
    private Button quit;
    public MyFrame(String title)
    {
        super(title);
        resize(500,300);
        setResizable(false);
        setLayout(new GridLayout(3,1));
        add=new Button("ADD");
        show=new Button("SHOW");
        quit=new Button("QUIT");
        add (add);
        add (show);
        add (quit);
    }
    public boolean action(Event evt,Object arg)
    {
        if(evt.target.equals(add))
        {
            String value1= JOptionPane.showInputDialog("Enter Car Model ");
            String value2= JOptionPane.showInputDialog("Enter Car Mark ");
            int value3= Integer.parseInt(JOptionPane.showInputDialog("Enter Kyvismos "));
            Integer I=new Integer(value3);
            Cars.add(new Cars(value1,value2,value3));//or pinakas.add(value);
        }
        else
        if(evt.target.equals(show))
        {
            String s="";
            int i;
            for(i=0;i<Cars.size();i++){
              **//here is the error on Cars.getkyvismos() or getmarka() or getmodelo();**
              //if(Cars.getkyvismos()>1900)
              s=s+Cars.getmarka()+Cars.getmodelo()+"\n";
            }
            JOptionPane.showMessageDialog(null, "cars with kyvismo >1900 are \n " + s);
        }
        else
        if(evt.target.equals(quit))
        {
            System.exit(0);
        }
        return true;
    }
}

Thanks in advance and sorry if its a newbie question!

foutzos
  • 71
  • 7

1 Answers1

3

The problem lies in that you have a class Cars and ArrayList instance Cars, and you are confusing them.

In this block from your example, Cars refers to the list, not the individual car.

for(i=0;i<Cars.size();i++){
**//here is the error on Cars.getkyvismos() or getmarka() or getmodelo();**
    //if(Cars.getkyvismos()>1900)
    s=s+Cars.getmarka()+Cars.getmodelo()+"\n";
}

The ArrayList Cars does not have a method getmarka().

You need something like

for(i=0;i<Cars.size();i++){
**//here is the error on Cars.getkyvismos() or getmarka() or getmodelo();**
  Cars car = Cars.get(i);  
//if(car.getkyvismos()>1900)
    s=s+car.getmarka()+car.getmodelo()+"\n";
}
bradimus
  • 2,472
  • 1
  • 16
  • 23
  • 1
    One thing I'd like to say for future reference is that instead of initializing `car` every time the code is run you could use a foreach loop: `for(Cars car : Cars)`. See more [here](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work). – No Name Feb 05 '16 at 21:24
  • @foutz I suggest that you start using the convention of naming classes starting with upper case and variable names with lower case. This will help you avoid these kinds of problems. – Code-Apprentice Feb 05 '16 at 21:39
  • @foutz Also, the difference between `car` and `cars` is so small that it is easy to get confused. Perhaps `carList` as a name would make the difference more clear so that you can avoid these kinds of mistakes. – Code-Apprentice Feb 05 '16 at 21:40