java is giving me the error that
Java\Workspace\PROJECTS\Farm\animalLab.java [line: 15]
Error: cannot find symbol
symbol: method addAnimal(java.lang.String,int,Farm[])
location: variable farm of type Farm[]
my code is
import java.util.Scanner;
class animalLab{
public Farm[] farm = new Farm[1000];
public void main(String[]args){
Scanner reader = new Scanner(System.in);
int i = 0;
while(reader.hasNext()){
if(reader.nextLine() == "Quit")
break;
if(reader.nextLine()=="Add"){
System.out.println("Enter animal to add: ");
String add = reader.nextLine();
farm.addAnimal(add, i, farm);
}
my farm class looks something like this
class Farm{
public String animal = null;
public Farm(String s){
animal = s;
}
public String getAnimal(){
return animal;
}
public void addAnimal(String add, int i, Farm farm[]){
for (int x =0; x<farm.length; x++)
if(farm[x] != null)
if(farm[x].animal.equals(add)){
System.out.println(add + " is already in the farm");
break;
}
farm[i] = new Farm(add);
}
farmis an array of (many) Farm objects.Arrays do not have an
addAnimalmethod. Take time to read the error message closely.Look at it like this…
Consider that
farm[x].animal"works" (the expressionfarm[x]is of typeFarm) — how can this be used to fix the compilation error?I would recommend naming variables for arrays/collection in a plural form — e.g.
farms— to mitigate this sort of confusion. Also, unless there is a requirement to use an array (such as a homework assignment), I would recommend usingArrayList— it makes adding and iterating items easier.Happy coding.