1

I am trying to create a program where the user inputs animal pertinent values, which then create animal Objects, which are then saved into an array list. The area I am having trouble with is shown below. After the Array list is populated, I cannot figure out how to use the user input (select) to find that values index within the array list. (int index = animalList.indexOf(?))

Would appreciate any help

Scanner in = new Scanner(System.in);
  List <Animal> animalList = new ArrayList <Animal>();
    char ans;

do{  // User input
  Animal animal = new Animal(); // arraylist

   System.out.println("Animal's 'common' name: ");
     animal.setName(in.next());

   System.out.println("Animal's class: ");
     animal.setAnmlClass(in.next());

   System.out.println("Vertabrate or Invertabrate: ");
     animal.setCharVert(in.next());

   System.out.println("Warm or Cold blooded: ");
     animal.setCharBld(in.next());

   System.out.println("Animal's habitat (general): ");
     animal.setCharHab(in.next());

   System.out.println("Would you like to enter in a new animal (y/n)? ");
     String answer = in.next();
        ans = answer.charAt(0);

    animalList.add(animal);

  }while(ans == 'y');

   System.out.println("Enter the animal you wish to view: ");
     String select = in.next();
     System.out.println(select);
     int index =  animalList.indexOf( ? );
     System.out.println(index);
2
  • indexOf won't get you what you want. Essentially, there's no way for the user to input a string that will end up satisfying Animal.equal(). Commented Jun 22, 2015 at 18:11
  • Does Animal class have overridden equals() based on name? Commented Jun 22, 2015 at 18:14

5 Answers 5

1

You might consider making a HashTable instead of a List, with the animal name as the key.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, the only problem is one of the specs for this project is using an Array List. If that wasn't a must, I would have gone with the Hash.
Use a HashMap<K,V> and not a HashTable<K,V>
0

Unfortunately, you need a custom method for this with a while loop:

public int getIndexWithName(List<Animal> animals, String name){
    for (int index = 0; index < animals.size(); index++){
       if (name.equals(animals.get(index).getName())){
           return index;
       }
    }
    return -1;
}

And use this:

int index =  getIndexWithName(animalList, select);
//Do some validation check
if (index < 0){
    //Give some error message
    System.out.println("That's not a common name I recognize!");
}

1 Comment

Thank you, I used a bit of what you suggested in my own way (had static non-static issues), anyways... I am a novice, so it may not be pretty, but it works... Thanks again
0
 System.out.println("Enter the name of the animal you wish to view: ");
 String select = in.next();
 System.out.println(select); // Check that your toString() its proper
 //There's not a straight way to find each animal's index
// so you 'll have to write an int getIndex(select) method to do that and then you 'll call it
 int index = getIndext(select);
 System.out.println("The animal you chose has index: "+index);

Comments

0

Create a HashMap<String, Animal> that maps an animal's name to the object.

Scanner in = new Scanner(System.in);
List <Animal> animalList = new ArrayList <Animal>();

Map<String, Animal> map = new HashMap<>();

char ans;

do{  // User input
  Animal animal = new Animal(); // arraylist

 System.out.println("Animal's 'common' name: ");

 String name = in.next();
 animal.setName(name);
 map.put(name, animal);

System.out.println("Animal's class: ");
 animal.setAnmlClass(in.next());

System.out.println("Vertabrate or Invertabrate: ");
 animal.setCharVert(in.next());

System.out.println("Warm or Cold blooded: ");
 animal.setCharBld(in.next());

System.out.println("Animal's habitat (general): ");
 animal.setCharHab(in.next());

System.out.println("Would you like to enter in a new animal (y/n)? ");
 String answer = in.next();
    ans = answer.charAt(0);

animalList.add(animal);

}while(ans == 'y');

System.out.println("Enter the animal you wish to view: ");
 String select = in.next();
 System.out.println(map.get(select));

Comments

0

With help from Roel & Theodora, I ended up with this. It may not be the cleanest or "prettiest" way to do this, but it works with no issues. System.out.println("Enter the animal you wish to view: "); String select = in.next();

 for (int index = 0; index < animalList.size(); index++){

 if (select.equals(animalList.get(index).getName())){
     System.out.println(index);

     System.out.format("%n"); // aesthetic break
     System.out.print(animalList.get(index).getName()+": ");
     System.out.print(animalList.get(index).getAnmlClass()+", ");
     System.out.print(animalList.get(index).getCharVert()+", ");
     System.out.print(animalList.get(index).getCharBld()+" Blooded, ");
     System.out.print(animalList.get(index).getCharHab()+" Terrain");
     System.out.format("%n"); // aesthetic break
   }
 else{
     System.out.println("The Zoo does not house that animal currently");
 }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.