• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you print out the array contents and display them here?
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80138
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic