Here's a snippet of some of my code: Now, all it does is keep adding (compounding) 875 dollars, which is the value in the last "if" statement. Why? Shouldn't it be adding the value of the other options when they're selected? Why isn't it?
This is making me crazy!
Thanks for your help.
[ June 15, 2008: Message edited by: Alex Bruhart ] [ June 15, 2008: Message edited by: Alex Bruhart ]
As previous posters have indicated, the main bug in the posted program is that there is a semicolon at the end of each "if" line, meaning that the following code is not controlled by the "if", but instead is always executed. To fix, remove those semicolons.
However, beyond that, there is plenty to improve in the posted code.
Rather than have a huge set of "if" statements, you could use a HashMap to map menu items (String) to cost (Integer).
Alternatively, as each menu item includes the price, you might be able to parse (read) the price within the menu item; look at the API for String.indexOf(), String.substring(), Integer.parseInt() etc. Or, if feeling brave, something similar but better can be achieved with a "regular expression"; see the Pattern and Matcher classes. [ June 16, 2008: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Also note that you can put arbitrary objects into a JList. If you have a class that has an appropriate toString method and, for example, a getPrice method, you could put those objects into the list, and use getSelectedValues to extract. Just iterate over them and ask them for their price - voila!
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus