This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Adding object into list through user input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding object into list through user input" Watch "Adding object into list through user input" New topic
Author

Adding object into list through user input

Val Simon
Greenhorn

Joined: Aug 06, 2012
Posts: 3
Hey everyone its my first post and im in desperate need of help. I'm currently working on a project in school. My instructor wants us to create an array with hard coded values and then add the objects from the array into the list. I've already created the hard-coded array and have added the objects into the list that I have created. Now what I want to do next is ask the user if they want to add more items, if yes then I have to add the items they have entered into my list, if no, I need to output a total for the cost.

Here is my code, I have an Item class and Sales class

So far this what I have for my Salclass


Thanks in advance for your input.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

You can receive all 4 inputs from user, and then create an Item object and populate its fields. Thereafter, you can add this new Item object to the list.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi
Val Simon Wrote:
I've already created the hard-coded array and have added the objects into the list that I have created.

there is no need for hard-coding. That may not help for adding the objects. Hope this code helps you.

Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

I know of companies ill-treating employees... but selling them outright! God help us all.

[EDIT] Trying to be more politically correct.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi Aditya Jha Good evening,

It is not to ill -treat. i Just didn't have time to change few things. and i am tied %')
Val Simon
Greenhorn

Joined: Aug 06, 2012
Posts: 3
deepika deepi wrote:hi
Val Simon Wrote:
I've already created the hard-coded array and have added the objects into the list that I have created.

there is no need for hard-coding. That may not help for adding the objects. Hope this code helps you.



My instructor wants the values to be hard-coded, its one of the specifications in this project. Thank you for the code, I will try to incorporate it with what I have.

Is there any way to add the user's input directly into my list?
I know this is wrong, but just for demonstration sake:



I need to keep adding stuff to my list until the user enters 'N'. Thanks
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
Val Simon Wrote:
I need to keep adding stuff to my list until the user enters 'N'.


you can use loop for checking the statement it it's true do some thing, when user types N break the statement



Hope this will help you to get the stuff till the user enters N.
Val Simon
Greenhorn

Joined: Aug 06, 2012
Posts: 3
deepika deepi wrote:
Val Simon Wrote:
I need to keep adding stuff to my list until the user enters 'N'.


you can use loop for checking the statement it it's true do some thing, when user types N break the statement



Hope this will help you to get the stuff till the user enters N.


I get an error on this bit of code

obj.add(answer);
**The method add(Item) in the type list is not applicable for the arguments(String)

Is it because answer is a String while my List is from my Item class? Is there any way around this? Thanks.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Aditya Jha wrote:You can receive all 4 inputs from user, and then create an Item object and populate its fields. Thereafter, you can add this new Item object to the list.

Does this sound feasible to you? If you have any doubts on any of these steps, do let us know.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Aditya Jha wrote: . . .] Trying to be more politically correct.
It never works

Careful about spelling: you have written quantity in one place and quality in another.
Have you really been told to create an Item class with a quantity field? The word item precludes any argument about quantity: an item is always one. You might buy several items of the same sort when shopping, but if I buy two bags of flour, for example, my receipt will look like this
cheese
butter
flour
flour
sugar
I think you need a class intermediate between item and order, which you might call line, which encapsulates the type of goods (item) and quantity, as long as you don’t buy more than 2147483647 at a time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Val Simon wrote: . . .

I get an error on this bit of code

obj.add(answer);
**The method add(Item) in the type list is not applicable for the arguments(String)

Is it because answer is a String while my List is from my Item class? Is there any way around this? Thanks.
No.
It is because you told the compiler you would only add Items to that list, and you are adding something different. Look at line 60 in your code. You need to pass the String or whatever to the Item constructor, and add the new Item object.
 
jQuery in Action, 2nd edition
 
subject: Adding object into list through user input
 
Similar Threads
Strange ViewParams, GET Requests, AJAX behavior
Sorry For Throwing Lengthy Codes At This Site, But I Really Need Help
Storing of Data
output prints null
Unable to import javabean class in servlet