• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


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 :

Posts: 3648
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Die Fledermaus does not fear such a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic