-2

First of all thanks everyone who is willing to read all this code and help me.

I have a Hashmap<String,Square> that simply prints a board to the console and it goes as follows:

public class Square {
private char status;

public Square(String statusp) {
    if(statusp.equals("empty")) {
        this.status = '.';
                    
    }else if(statusp.equals("black")) {
        this.status = 'X';
        
    }else if(statusp.equals("white")) {
        this.status = 'O';
        
    }else {
        System.out.println("ERROR: status can only be: empty, black, white");
    }
}

public char getStatus() {
    return status;
} 

}

public class Board {
public HashMap<String, Square> board;

public Board() {
    board = new HashMap<String, Square>();
    Square empty = new Square("empty");
    Square black = new Square("black");
    Square white = new Square("white");
    
    
    board.put("a1", empty);
    board.put("a2", empty);
    board.put("a3", empty);
    board.put("a4", empty);
    board.put("a5", empty);
    board.put("a6", empty);
    board.put("a7", empty);
    board.put("a8", empty);
    
    board.put("b1", empty);
    board.put("b2", empty);
    board.put("b3", empty);
    board.put("b4", empty);
    board.put("b5", empty);
    board.put("b6", empty);
    board.put("b7", empty);
    board.put("b8", empty);
    
    board.put("c1", empty);
    board.put("c2", empty);
    board.put("c3", empty);
    board.put("c4", empty);
    board.put("c5", empty);
    board.put("c6", empty);
    board.put("c7", empty);
    board.put("c8", empty);
    
    board.put("d1", empty);
    board.put("d2", empty);
    board.put("d3", empty);
    board.put("d4", white);
    board.put("d5", black);
    board.put("d6", empty);
    board.put("d7", empty);
    board.put("d8", empty);
    
    board.put("e1", empty);
    board.put("e2", empty);
    board.put("e3", empty);
    board.put("e4", black);
    board.put("e5", white);
    board.put("e6", empty);
    board.put("e7", empty);
    board.put("e8", empty);
    
    board.put("f1", empty);
    board.put("f2", empty);
    board.put("f3", empty);
    board.put("f4", empty);
    board.put("f5", empty);
    board.put("f6", empty);
    board.put("f7", empty);
    board.put("f8", empty);
    
    board.put("g1", empty);
    board.put("g2", empty);
    board.put("g3", empty);
    board.put("g4", empty);
    board.put("g5", empty);
    board.put("g6", empty);
    board.put("g7", empty);
    board.put("g8", empty);
    
    board.put("h1", empty);
    board.put("h2", empty);
    board.put("h3", empty);
    board.put("h4", empty);
    board.put("h5", empty);
    board.put("h6", empty);
    board.put("h7", empty);
    board.put("h8", empty);
    
    
    
}

}

And it basically prints this: Board

The idea is that when the user gives an input that is equal to one of the keys of the HashMap the value of that key will change to either Square black or Square white (X or O) AND the value only changes if the users inputted key doesnt already have the value of Square black or Square white. The first check works fine but i cant seem to get the second check working. It always overwrites the value even if it already was X or O.

To perfom the above checks i have the following code:

System.out.println(p1.getName().toUpperCase() + ", please enter your move:");
    String move = io.readInput();

        //check if the hashmap contains the key of move
    if (board.board.containsKey(move)) {
        
        //check if the value of key move isnt equal to Square black or white (X or O)
        if(board.board.get(move) != black || board.board.get(move) != white ) {
        
        board.board.replace(move, black);
        }else {
            System.out.println("that spot is already taken");
        }

        board.printBoard();
    }
    

I tried changing the || to && but no results. i also tried:

if(board.board.get(move) == empty)  {
        
        board.board.replace(move, black);
        board.printBoard();
        }else {
            System.out.println("that spot is already taken");
        }

But then no matter what input i give it always returns the else statement and the if statement is never true even if move equals empty.

Any idea why the second if statement doesnt work? it gives me zero erros.

6
  • Did you copy/paste the same text from your previous question that got closed an hour ago? Commented Jan 9, 2021 at 19:19
  • 1
    Gut feeling: You should implement equals and hashCode for your custom Square class. Commented Jan 9, 2021 at 19:19
  • Does this answer your question? Java Hashmap value check Commented Jan 9, 2021 at 19:20
  • @fluffy: If that were the case then flagging the question would be appropriate. Honestly just looking at it I can't see anything particularly wrong with this question that would warrant its closure. Commented Jan 9, 2021 at 19:20
  • @Makoto The only difference between these two is that the newer one lacks the rendering code (actually the printBoard method). Commented Jan 9, 2021 at 19:24

2 Answers 2

1

to start, I suggested you do the following test (does not solve the question, but eventually shows the real problem - not related to HashMap):

System.out.println(new Square("black") != new Square("black"));

to compare objects (by content) you should use equals, like is actually being done inside Square. Reason: == and !=, when applied to objects, do not compare the content, but only if it is the same instance (same memory) or not. new will always, if not terminated abruptly, create a new instance. Same reason for new String("abc") != new String("abc")

Solution: add/implement/override the equals method in Square AND use it to compare them. This method would compare the value stored in status.

Note: when overriding the equals method it is also recommended to override the hashCode method.


Workaround: declare constants - only once, globally available, used everywhere:

public final Square BLACK = new Square("black");
...

if used consistently, these can be compared with == and != - implementing equals is still recommended.

Since this is a dangerous solution, some developer may create a new instance and it will fail again... see next solution.


Better solution: create an enum for this 3 values:

public enum Square {
    EMPTY, BLACK, WHITE;
}

no need for equals and can be compared directly with == and != (or equals) (but will need to override toString or implement some method for correct output)


assuming code has different instances of white, black and empty, based on board.board.get(move) == empty always returning false

Note: also check Joop's answer!!

Sign up to request clarification or add additional context in comments.

Comments

0
if (board.board.get(move) != black || board.board.get(move) != white)

This pattern: !CASE_A || !CASE_B is always true (as one of the cases will be false). Use

if (board.board.get(move) != black && board.board.get(move) != white)

2 Comments

I tried that, but then it still overwrites the value even if its already black or white.
Make empty, black and white final fields, constants. Object identity, == resp. != against equals.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.