-7

Pretty simple, Would

if(clickedButton == button0 || button1 || button2 || button3 || button4 || button5 || button6 || button7 || button8 || button9)

return true if I had clicked button0, button1, or button2 etc? Also, I don't understand why if(clickedButton == (button0 || button1 || button2)... etc. doesn't work either. Is it the same thing (checking for true or false on a JButton) or is something else completely. Mostly I'm just messing around with parenthesis, so when this thought hit me, I tried it, and I don't understand why it doesn't work.

IntrepidPig
  • 110
  • 2
  • 12

6 Answers6

7

No, that would be a compilation error since it is parsing as

if ((clickedButton == button0) || (button1) || (button2) ...

and buttons are not booleans.

You must do:

if (clickedButton == button0 || clickedButton == button1 ...

But an array would be much cleaner, instead of having nine separate button variables. Then you could do this:

if (Arrays.asList(buttons).contains(clickedButton)) {
    ...
}

Or, if your buttons are stored in an ArrayList (or any List), it's just

if (buttons.contains(clickedButton)) {
    ...
}
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • +1 for array/list solution – Alexis C. Dec 16 '13 at 14:04
  • +1: A nice solution that _does_ condense on the if-statement, which minimizes the chance of mistakes/typos! – Chris Forrence Dec 16 '13 at 14:06
  • Hmmm, `Arrays.asList ...` requires a sequential scan for every invocation - a `Set` would be more elegant. – Gyro Gearless Dec 16 '13 at 14:06
  • 1
    That's why I love stackoverflow, each time you think you know the best solution there is someone with even better answer. – mdolbin Dec 16 '13 at 14:07
  • @GyroGearless Do you think that's an appropriate optimisation, even if it wasn't creating the list every time? – Tom Hawtin - tackline Dec 16 '13 at 14:08
  • @TomHawtin-tackline : Maybe i'm a bit old-fashioned, but if i have the choice of solving a problem in O(1) rather than O(n), i'll choose O(1), especially if it's low hanging fruit like replacing a `List` with a `Set`. On the other hand, given todays computers speed and the hopefully limited number of buttons, it will perhaps make no noticeable difference in practise... – Gyro Gearless Dec 16 '13 at 14:43
  • @GyroGearless `n` is 10. The code is simpler if you create the collection with `asList`. You could compute the collection ahead of time, but that's often far, far worse as in the interactive world the setup is typically under more extreme time-pressures and you're dragging this object around for an unnecessary amount of time. Optimising this sort of thing for machine performance is really bad. – Tom Hawtin - tackline Dec 16 '13 at 14:55
2

The short answer is no. You would have to do

if(clickedButton == button0 
    || clickedButton == button1
    || clickedButton == button2 
    || clickedButton == button3
    || clickedButton == button4
    || clickedButton == button5
    || clickedButton == button6
    || clickedButton == button7
    || clickedButton == button8
    || clickedButton == button9)
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
2

No.

A boolean expression is expected between logical operators. I assume in your example that button1,...,button9 are not of a boolean type.

Thus this will lead to an exception.

Loïc Gammaitoni
  • 4,173
  • 16
  • 39
1

Almost certainly a better approach would be to modify your action listener (or equivalent). Checking the source of an event tends to lead to messy and fragile code.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0
  1. You need to compare each variable separately

    if(clickedButton == button0 || clickedButton == button1 ..... )

  2. Use equals() instead of ==

Reason

equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects!. That means, the contents of the objects. Whereas the == operator is expected to check the actual object instances are same or not.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
  • This is perfeclty fine to use `==` to compare buttons. – Alexis C. Dec 16 '13 at 14:07
  • For button it is fine. I just remember using equals() is better – Rakesh KR Dec 16 '13 at 14:08
  • It's certainly good to remember to use `equals()` when appropriate (there are so many questions where the asker incorrectly uses `==` to compare Strings). While not needed in this case, your mention of it certainly is good. – Chris Forrence Dec 16 '13 at 14:20
0

Additional side note: If the type of your button is anything that can be used in a switch-Statement, e.g. ints, longs, String, enums (and your buttons button0, ..., button9 are constants), you can make use of the (often discouraged) fall-through mechanism:

switch(clickedButton) {
  case button0:
  case button1:
  case button2:
  case button3:
  case button4:
  case button5:
  case button6:
  case button7:
  case button8:
  case button9:
    // do whatever you wish
}

In this scenario, the last (commented) line will be executed whenever clickedButton is one of button0, ..., button9.

jpvee
  • 913
  • 1
  • 15
  • 23