aspose file tools*
The moose likes Beginning Java and the fly likes Totally lost on how to add objects into a array via userInput Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Totally lost on how to add objects into a array via userInput " Watch "Totally lost on how to add objects into a array via userInput " New topic
Author

Totally lost on how to add objects into a array via userInput

Sam Acropolis
Greenhorn

Joined: Sep 30, 2012
Posts: 16
Guys Im out of ideas on this one and decided to turn to the community. I working on a program that models a PC shop where the user picks the parts he wants for a laptop or desktop and as he picks i add up the total and display the results at the end of the program. I can prompt the users with a greeting and have the program run through selecting the RAM, HDD, and CPU but at line 119 the magic stops and no matter if I type "Y" or "N" the program is stuck at that particular loop.

No idea now how to have the objects selected with their made up prices get totaled and then sent to displayResults

Thanks,


Here is what I have so far:


this is my main super class, and i also have subclasses "ComputerList"(so I can add objects in the array) , "Desktop" and "Laptop"




This my ComputerList class which Im having the issues and am at a dead end :





K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2503
    
    8

Welcome to the Ranch.

Base on what you have already, there can be lots of improvement to make it better mainly in organizing the code (eg OO).

Why would work with an "array" when there are list and map collections? Your ComputerList class primary purpose is to store those 5 components. If I were you, using a map seems to be better. This way you can identify what each item and their corresponding values. With arrays, you must stick to the order you add them which can become messy. In fact, your ComputerList class as separate entity is really pointless I think. You can just define a list or map or array inside the Computer class and add to it when you know the item/value. Again order of adding items is key for list and array.

As for method DriveOption() in class Computer, you have userInput.next() twice. You can remove the first one, keep the one inside the do-while loop. Not sure if this will solve your loop problem.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Totally lost on how to add objects into a array via userInput