java - Get method from a class on an Arraylist -
i made class returns 3 values of arraylist main program when use get()
method compiler throws me en error cant find method - symbol. tried same thing doing not arraylist array , seems work. cant figure way array list.
//error on "action evt" show button on second mark of code.
error msg is: "cannot find symbol-method getmarka()".
i posting code below maybe can me.
the class method returns 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 class gets 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 error on cars.getkyvismos() or getmarka() or getmodelo();** //if(cars.getkyvismos()>1900) s=s+cars.getmarka()+cars.getmodelo()+"\n"; } joptionpane.showmessagedialog(null, "cars kyvismo >1900 \n " + s); } else if(evt.target.equals(quit)) { system.exit(0); } return true; } }
thanks in advance , sorry if newbie question!
the problem lies in have class cars , arraylist instance cars, , confusing them.
in block example, cars
refers list, not individual car.
for(i=0;i<cars.size();i++){ **//here error on cars.getkyvismos() or getmarka() or getmodelo();** //if(cars.getkyvismos()>1900) s=s+cars.getmarka()+cars.getmodelo()+"\n"; }
the arraylist cars
not have method getmarka()
.
you need
for(i=0;i<cars.size();i++){ **//here error on cars.getkyvismos() or getmarka() or getmodelo();** cars car = cars.get(i); //if(car.getkyvismos()>1900) s=s+car.getmarka()+car.getmodelo()+"\n"; }
Comments
Post a Comment