1

In the following example I am trying to make sure if a clicked button is included inside a certain JButton array. When pressing the button in the screen I just getting the string false which means the while loop evaluates to false. I think if I am not wrong that this is because Arrays.asList(ArrName).contains(element) doesent apply to primitive types. If my guess is true how to solve this issue? If it is not true what is the solution for this ?

            JButton[] num;
            c = ((JButton) e.getSource()).getText();
            System.out.println(Arrays.asList(this.num).contains(c));
            while(Arrays.asList(this.num).contains(c)){
                System.out.println("True");
            }
  • [`ArrayList.contains(Object o)`](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#contains-java.lang.Object-)? Or were you looking for something else? – Arc676 Jan 09 '16 at 10:43
  • 2
    Possible duplicate http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value – Ali Dehghani Jan 09 '16 at 10:50

1 Answers1

0

It won't work if you try to find a string in a JButton array.

Change c = ((JButton) e.getSource()).getText(); to c = (JButton) e.getSource();

Gaktan
  • 458
  • 3
  • 9