• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Some help with getting an ArrayList.  RSS feed

 
Dylan Stout
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run my program It works up until it asks for an animal.
I am really stuck on this!
How do I get the type of Animal I type to come up? It just keeps going back to the main menu part.

here is my Animal1 class

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your list is empty so you won't find anything inside it.

Some points about your code:
You need to fix indentation.
What is setName method supposed to do? It takes four parameters and sets the name of Animal1 to the last one. The rest is ignored.
This is how you usually write a setter:Variable names should begin with lower case.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, don't use imports like:
Better is to import the classes you're going to use explicitly, it would improve your code readability.

Use:
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use parseInt along with a Scanner. The Scanner can do all the hard work: look for its nextXXX methods.
 
Dylan Stout
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't use parseInt along with a Scanner. The Scanner can do all the hard work: look for its nextXXX methods.

My reasoning behind using the paresInt is for when you have your choose picking 1-3 (Im going to add more once I get this to work) That if you type anything but a number for that part it System.outprints "Enter a number not a letter.

Paweł Baczyński wrote:Your list is empty so you won't find anything inside it.

Some points about your code:
You need to fix indentation.
What is setName method supposed to do? It takes four parameters and sets the name of Animal1 to the last one. The rest is ignored.
This is how you usually write a setter:Variable names should begin with lower case.


I changed the part for it looks like this now:

when it is run I type in Monkey but it doesn't outprint "
Here is my code for this:
>
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dylan Stout wrote:. . . if you type anything but a number for that part it System.outprints "Enter a number not a letter." . . .
But you should not do that with an Exception. Use the hasNextXXX methods of the Scanner.

It is bad practice to use an Exception for flow control if you can use an if‑else instead.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the method parameter is named Monkey? It has nothing to do with string Monkey you try to type.
The name of method parameter do not affect the program in any way.

And your list is still empty.
You won't find anything in an empty list. You need to create instances of Animal1 class and add them to the list.

By the way, why is the class named Animal1 and not Animal?
 
Dylan Stout
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I guess I am a little confused on where my list needs to go?
Where do I put the list so that when I type the the Search for my keyboard matches it from my list?
How do I make an Array list and choose from one of them and where do I store it for newAnimal.get(i).getName() to grab what I typed in? If I had DOG and CAT do I put them in my Animal1 class as an Arraylist or?


Also it is named Animal1 because I was working on another class called Animal and it wasn't working and I was just testing two different things at once basically going back and forth between the two, when I'm all done I will be fixing that and the Spacing in my code.
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add elements to the list, use the add() method.


 
Dylan Stout
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:To add elements to the list, use the add() method.



I am somewhat familiar with the add() method, but when I put multiple animals and I type it, it works but this shows up
First here is the code

When I run the program It asks me to input an animal I pic say cat and this is what comes up:

How to do I get it to not show up all the other Not found Animals?
Eventually I will have to make a code that once you Pick an animal it will display information about them
for example:

Ultimately this is something I am trying to get at
I need to store different Animals in an Array Along with information about each animal Stored!
But then I have to have an edit mode on the Main Menu part of my Code which will be number 3. And I have no clue at all how to begin to do that!?
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to do I get it to not show up all the other Not found Animals?

Here's what you wrote (with formatting):

What if you set a flag in the for loop and checked the flag after the for loop?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!