• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Objects into an Array, or ArrayList from user input  RSS feed

 
J. Chris Miller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm "New?" to Java..Actually I've been trying to learn it for years, but I'm apparently a snail when it comes to programming.

Anyway, I'm trying to make a program that can create and store tools at a hardware store. I have a Main class, Tool Class, Hammer Class (which extends Tool Class)

From user input, I want to create a Hammer, and put it into the array or ArrayList for future use. A tool, and a hammer for that matter have multiple attributes (String brand, double price, int sku)

One problem is that I don't know how to create a new object to put into the array with a unique identifier. Once I've created a new Hammer, how do I take user input and customize the hammer with brand, price, sku? Any help would be greatly appreciated.

This is what I have so far:

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all tools have a brand, price and sku (whatever that is?) then ask the user to enter values of all those categories before trying to create any tool and pass the values into the tools constructor so your object is fully initialized when it is constructed.

BTW I suggest you change the to so that it doesn't matter what case the user uses when entering the tool name.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
J. Chris Miller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I do that?


Sorry, I really have a hard time finding information about this. Yes, I've made like 10+ google searches on the topic. I just need to know how to take user input and go through the constructor to make a new object, and add that object to an array.


 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you confine yourself to hammers only, for a bit. Create a simplified Hammer class, which only has price as a field. Get used to creating Hammer objects. Then try putting them into the List.
Once you have got used to that sort of thing, you can reintroduce the other fields.
You do realise that the loop you have will fill your array entirely with hammers, even if you might want drills or saws in it?
There is something not quite right about entering types, e.g. Hammer, as text and then checking the names. I am not sure what the best solution would be.
 
J. Chris Miller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I already know how to do that.



The problem I'm having is a bit more dynamic. I need to know how to make a tool with user input with several fields, and then add that to an array or ArrayList.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to know how to make a tool with user input with several fields,

You store each of the input values in a different variable ie called type, brand, price, sku and code the constructor of your Hammer class so that it accepts three parameters ie brand, price and sku.
Then when create an instance of Hammer you pass in the values ie
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Chris Miller wrote:I need to know how to make a tool with user input with several fields, and then add that to an array or ArrayList.

Note that this is (at least) three distinct problems:

1) How to make a tool with several fields
2) How to get user input
3) How to add your object to an array.

Each of these can (and SHOULD) be done completely independently. Assuming your tool has a type, brand, and cost, it doesn't matter where those values come from - they could come from user input, from a database read, from a file, or even be hard-coded. Figure out how to create your tools first.

THEN figure out how to get user input.

THEN figure out how to create the tool using your user's input.

 
J. Chris Miller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, this is what I've got. I'm able to create a tool with user input with 2 fields.

I now just need the syntax for adding them to an arrayList. The areas where I need help are commented.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this line:says "I am making an ArrayList that can hold String objects."

Is that what you want? Or do you want an ArrayList that can hold something else...like perhaps a SimpleTool?
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should also not have that much code in your main method. You should have methods in the tool class, and methods in the store class, those being called from elsewhere.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: Or do you want an ArrayList that can hold something else...like perhaps a SimpleTool?

Fred, can you tell how to make the ArrayList hold something like SimpleTool?
actually i knew how to do it but i forgot perhaps you can shed some light on it
Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Fred, can you tell how to make the ArrayList hold something like SimpleTool?
Thanks

I thought my previous post spelled it out...

if this:

ArrayList t = new ArrayList<String>();

makes an ArrayList that can hold Strings,

what do you think you should do to make an ArrayList that can hold SimpleTools?
 
Marshall Brett
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



What you're doing here is creating a new object of type SimpleTool. The fact that you've made a class for this object means you can declare an array of type SimpleTool.



Right?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshall Brett wrote:


What you're doing here is creating a new object of type SimpleTool. The fact that you've made a class for this object means you can declare an array of type SimpleTool.



Right?



Well, what happened when you tried it? Did the compiler complain when you tried to add x to tools? Did you get an error when you ran the code?

IMO, the best way to learn something (at least so that it sticks) is hands-on experience.

Henry


[EDIT: I thought this post was from the OP -- it is not. And as such, it can be read as another rancher summarizing, and not asking for elaboration. So, apologies if this post makes an assumption that isn't true.]

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!