user3267882
user3267882

Reputation: 117

Java Changing State

i represents rows and y represents columns in a connect 4 game, I am trying to change the color of the circle depending on the column the user has selected but this code keeps giving an error type mismatch cannot convert state to string.

public enum State{
    RED, YELLOW, BLANK;
}

Upvotes: 2

Views: 89

Answers (3)

Martin Dinov
Martin Dinov

Reputation: 8825

You can't directly compare an enum value and a string, or assign an enum value to a string. However, you can convert the enum into a string and compare them then:

if (f[i][y].equals(String.valueOf(State.BLANK)) {

Similarly for the next line:

f[i][y] = String.valueOf(State.RED);

Upvotes: 0

diazazar
diazazar

Reputation: 542

You try to assign an enum to a string.
try f[i][y]=State.RED.toString() for saving it and State.valueOf(f[i][y]) to get the enum from a string. Another approach is to have an array of enums(just use search, i'm sure you will find something)

Upvotes: 0

Maroun
Maroun

Reputation: 95968

Exactly as the error message says, f should contain Strings, as your declaration states:

public static void dropRedCounter (String[][] f)
                                   ↑    

But you're comparing its value to a State, and you're also trying to assign a State to it:

f[i][y] = State.RED;

You might want to have State.RED.name() in order to have the String value of the Enum.

See the docs for further details: Enum Types.

Upvotes: 2

Related Questions