views:

124

answers:

3

I am writing an application for a Java course. I am a complete beginner and am just going off material I have learned from the course and from the web. The application is exhibiting some behavior and I am not sure what is causing it. The application is GUI based and does calculations on user input. For the action listener section, I have a set of If statements such as: "if this button do this if this button do this"

All in a row like that. It seems as if the application is running ALL the if statements instead of running the one that corresponds with the button pressed.

Would I be better off using a case/switch structure for this sort of thing?

I can post my code if necessary, I am new around this site and am not sure if that thing is acceptable.

+1  A: 

You probably shouldn't have a single action listener--creating a separate one for each control helps keep your code more readable.

If your controls are sharing a lot of code, however, (the stuff before or after the if statements) then it might make sense to do it that way. In that case, it should be if/elseif.

Also, making your listeners full-fledged classes (rather than anonymous inner classes) can help reuse code as well (the stuff inside each if statement goes into each subclass). This may be beyond what you've learned so far.

Edit: (more direct answer to your actual question)

As far as your problem, the if's should not all execute unless either drs9222's answer that said you were using a semi-colon at the end of your if is correct, or your if statement is testing the wrong thing.

You might just post what you are testing, but you need to compare your known "Button" object to event.getSource() using == or .equals. Since each Button object you are comparing to is different, only one should execute.

Bill K
I figured since the application was so small, a single listener would be ok. Since then, it has grown somewhat though and I may do just that in the end. In the spirit of learning though, I would like to figure out why my if statements are acting so weird. Making subclasses has crossed my mind as well. Thanks for your input.
+1 for one action, one action listener. -10 for "full-fledged classes".
Tom Hawtin - tackline
@tom Anonymous inner classes are not very good with code reuse. Few things aren't improved by making them a full, reusable class with members and all. I've often refactored quite a bit of code written by people with attitudes like that into a single class 1/10 the size because they never considered that listeners should more often than not be full classes and therefore couldn't see the refactor and just did the evil copy/paste/edit/repeat trick.
Bill K
+2  A: 

Without seeing the code I can't say for sure, but something I always used to do was just use if and not else if - if several of my conditions were satisfied, then all of the associated code was run. If you want only the first match to run, use if-else to ensure that none of the following statements will execute.

Andy Mikula
The code can be found here http://pastebin.com/m70593cb3There are 3 if statements for a drop down menu, and each button has their own if statement, it may make more sense if you see the code. Thank you for your insight thus far.
+3  A: 

Until I see your code I'll have to guess but given your admitted newness you may be writing your if statements like this

if (condition);
{
    ...
}

instead of like this

if (condition)
{
    ...
}
drs9222
I double checked as I realize this may cause problems as I have made that mistake before, but my statements do not end with semicolon. the code can be found here http://pastebin.com/m70593cb3 if you care to take a peak
After a very quick glance I see that lines 141, 169 and 198 have this problem
drs9222
ahh, how did I miss that multiple times... sorry I was just looking over it again and saw them there..my eyes must be getting lazy ;) thank you
That isn't a problem if you use the Java style and always use braces.
Tom Hawtin - tackline
Sure it is. Jesse had braces on the if statements. Your thinking of the more common issue of starting with code like "if (expr) stmt;" and then changing it to "if (expr) stmt; stmt2;" instead of "if (expr) { stmt; stmt2; }"
drs9222