wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes This is making me crazy, and it should be so simple! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "This is making me crazy, and it should be so simple!" Watch "This is making me crazy, and it should be so simple!" New topic
Author

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

Alex Bruhart
Greenhorn

Joined: Apr 20, 2008
Posts: 8
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
Can you print out the array contents and display them here?
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

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
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
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alex Bruhart
Greenhorn

Joined: Apr 20, 2008
Posts: 8
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

Joined: Oct 13, 2005
Posts: 37970
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: This is making me crazy, and it should be so simple!
 
Similar Threads
GUI Pieces and Parts Not Sticking! Help!
About Operator Precendence
What equals 100% in life?
JComboBox not entirely working
Euler Problem One