Beginner Java arraylist cannot find symbol method(int)

onesnapthanos :

I am having an issue with my displayIndex method below, I am getting an error on Cat cat = cat.get(index);saying cannot find symbol - method get(int), I thought this was the correct syntax when using .get(index) from the arrayList?

import java.util.ArrayList;
public class Cattery
{
    // instance variables 
    private ArrayList<Cat> cat;
    private String businessName;
    /**
     * 
     */
    public Cattery(String businessName){
        cat = new ArrayList<Cat>();
        this.businessName = businessName;
     }
    /**
     * 
     */
    public void addCat(Cat newCat){
     cat.add(newCat);
     }
    /**
     * 
     */
    public void displayIndex(int index) {
    if((index >= 0) && (index <= cat.size()-1)) {
        Cat cat = cat.get(index);                
        System.out.println(index);
    }
    else{
        System.out.println("Invalid index position");
    }
    }
    /**
     * to remove cat
     */
     public void removeCat(int indexRemove){
       if((indexRemove >= 0) && (indexRemove <= cat.size()-1)) {
         cat.remove(indexRemove);
        }
     else{
         System.out.println("Invalid index position");
       }
       }

    public void displayNames(){
    System.out.println("The current guests in Puss in Boots Cattery:");
    for(Cat catNames : cat ){
       System.out.println(catNames.getName());

       }
    }
}
RR_IL :

You declare cat here

  private ArrayList<Cat> cat;

and then again here (Hence the conflict)

public void displayIndex(int index) {
if((index >= 0) && (index <= cat.size()-1)) {
    Cat cat = cat.get(index);    

You can change it to

 public void displayIndex(int index) {
 if((index >= 0) && (index <= cat.size()-1)) {
 Cat cat = this.cat.get(index);     

Otherwise rename the variable.

public class Cattery
{
    // instance variables 
    private ArrayList<Cat> cats;
    private String businessName;
    /**
     * 
     */
    public Cattery(String businessName){
        cats = new ArrayList<Cat>();
        this.businessName = businessName;
     }
    /**
     * 
     */
    public void addCat(Cat newCat){
     cats.add(newCat);
     }
    /**
     * 
     */
    public void displayIndex(int index) {
    if((index >= 0) && (index <= cats.size()-1)) {
        Cat cat = cats.get(index);                
        System.out.println(index);
    }
    else{
        System.out.println("Invalid index position");
    }
    }
    /**
     * to remove cat
     */
     public void removeCat(int indexRemove){
       if((indexRemove >= 0) && (indexRemove <= cats.size()-1)) {
         cats.remove(indexRemove);
        }
     else{
         System.out.println("Invalid index position");
       }
       }

    public void displayNames(){
    System.out.println("The current guests in Puss in Boots Cattery:");
    for(Cat catNames : cats ){
       System.out.println(catNames.getName());

       }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344741&siteId=1