This is making me crazy, and it should be so simple!
Alex Bruhart
Greenhorn
Joined: Apr 20, 2008
Posts: 8
posted
0
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 ]
Robert Hill
Ranch Hand
Joined: Feb 24, 2006
Posts: 94
posted
0
Can you print out the array contents and display them here?
Wirianto Djunaidi
Ranch Hand
Joined: Mar 20, 2001
Posts: 195
posted
0
It is because you have semicolon at the end of if, so basically you are terminating the if without condition statement to be executed.
The curly braces after the if in your code is just treated as a scoping block.
You need to remove all the semicolon at the end of your if statement to let the compiler see the following braces block as the condition statement to be executed.
-DJ
imaya Munusamy
Greenhorn
Joined: May 19, 2008
Posts: 20
posted
0
There is semicolon at the end of every "if" statement so the indCost = value; is not part of the if block.
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
posted
0
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.<br /> <br />#:^P
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
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