• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This is making me crazy, and it should be so simple!

 
Alex Bruhart
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you print out the array contents and display them here?
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you don't want to override toString, or you can't, you can use a ListCellRenderer instead to do the rendering for you.
 
Alex Bruhart
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh wow, call me stupid. I didn't even realize I put a semi-colon at the end of each if statement.

Thanks for the other tips too guys, I appreciate it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alex Bruhart:
I didn't even realize I put a semi-colon at the end of each if statement.
Everybody does that at some time, so don't worry about it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic