0

I keep getting a false boolean value from the following comparison:

public boolean getHz1() {
    return board[0][0] == board[0][1] 
           && board[0][1] == board[0][2] 
           && board[0][0] != null 
           && board[0][1] != null 
           && board[0][2] != null;
 }

The relevant call in the driver class is:

do {
        System.out.println("Your turn: ");
        g1.play(s.nextInt(), s.nextInt(), s.next());
        System.out.println(g1.getHz1());
} while (g1.getHz1() == false)

The "play" method takes 2 integers and a string value to put in the array, in this case "x". When I print out the board[0][0], board[0][1] and board[0][2], all contain the string "x", but getHz1 still returns false.

saw303
  • 8,051
  • 7
  • 50
  • 90
user1660886
  • 69
  • 1
  • 2
  • 8
  • Can you try with equals method instead of comparing values with == operator – Nagendra Varma Dec 07 '17 at 04:51
  • 1
    (1) You usually need to use `equals` instead of `==` to compare String values. (2) You should do the null checks before the equality checks, not the other way round, or you risk getting null pointer exceptions. – Dawood ibn Kareem Dec 07 '17 at 04:51

3 Answers3

0

As mentioned in the comments you should compare objects using the equals method instead of ==.

public boolean getHz1() {
    return board[0][0] != null 
           && board[0][1] != null 
           && board[0][2] != null
           && board[0][0].equals(board[0][1]) 
           && board[0][1].equals(board[0][2]);
}

== is only true if two objects have the same pointer.

saw303
  • 8,051
  • 7
  • 50
  • 90
0

"==" is only used for primitive types (ints, booleans, etc.). A string is an object, so using == is checking the object reference (if String 1 is the same object as String 2). When comparing strings, you need to use .equals(), which tests for equal values.

saw303
  • 8,051
  • 7
  • 50
  • 90
JoJo
  • 117
  • 3
  • 12
0

As others have mentioned, you need to use .equals for objects (e.g., String, HashMap, anything that is created with new), and the == operator for native datatypes (e.g., int, long, char, etc.). The warning, however, is that .equals is a function on an object, so you first need to make sure the object is not null. Hence this pattern:

if ( obj != null && obj.someFunction() )

When comparing objects, you can also let the .equals function do the null check for you:

SomeObject goodObj = new SomeObject();
SomeObject badObj = null;

if ( goodObj.equals( badObj ) )  // should work properly, no matter the state of badObj

In your case, you need to check that your objects are not null first, and then compare the objects appropriately with .equals:

public boolean getHz1() {
    return (
        (null == board[0][0] == board[0][1] == board[0][2])
     || (null != board[0][0] && board[0][0].equals(board[0][1]) && board[0][0].equals(board[0][2]))
    );
}

Presumably, if they're all null, they're also all equivalent, hence the first line. The next line checks the only other way they could be equal (they're all not null, but arbitrarily check the first cell for nullness).

hunteke
  • 3,648
  • 1
  • 7
  • 17
  • The following works well: public boolean getHz1() { return board[0][0] != null && board[0][1] != null && board[0][2] != null && board[0][0].equals(board[0][1]) && board[0][1].equals(board[0][2]); – user1660886 Dec 07 '17 at 05:47