0

Lets say I have a public class called GameBoard that will be a two dimensional array with 4 rows and 5 columns. The spaces in the array are filed with String values from 1 to 20. A card will be drawn that has a name (King of Spades for example) . If the user inputs 15 I will store it in a String variable called userLocation. What would be the most efficient way to create a method that takes the input location and updates the array with the name of the Card? Would a for loop be most efficient?

public GameBoard() {
    square  = new String[4][5];
    square[0][0] = new String("1");
    square[0][1] = new String("2");
    square[0][2] = new String("3");
    square[0][3] = new String("4");
    square[0][4] = new String("5");
    square[1][0] = new String("6");
    square[1][1] = new String("7");
    square[1][2] = new String("8");
    square[1][3] = new String("9");
    square[1][4] = new String("10");
    square[2][1] = new String("11");
    square[2][2] = new String("12");
    square[2][3] = new String("13");
    square[3][1] = new String("14");
    square[3][2] = new String("15");
    square[3][3] = new String("16");
    square[2][0] = new String(17);
    square[3][0] = new String(18);
    square[2][4] = new String(19);
    square[3][4] = new String(20);

}

My preferred method as of now would look something like this but it gives me the error code "type mismatch:cannot convert string to boolean" under userLocation = board[i][j]

public String[][] updateBoard(String userLocation, Card card, String[][] board) {

    for (int i = 0; i <4; i++)
        {
           for (int j = 0; j < 5; j++)
           {
             if(userLocation = board[i][j]) {

                 board[i][j] = card.name;

             }
           } 
        } 

    return board;

}
5
  • == not = is what you need to make it compile, but use .equals to compare strings. So (userLocation.equals(board[i][j])) Commented Feb 17, 2020 at 4:40
  • @JeremyKahan thank you pointing out that simple mistake! Under board I now have an error saying "this type of expression must be an array type but it resolved to GameBoard". Do you see anywhere in my syntax that I should change to resolve this error code? Commented Feb 17, 2020 at 4:45
  • did you pass in square to updateBoard? Commented Feb 17, 2020 at 4:48
  • @JeremyKahan in the statement userLocation == board[i][j] the board[i][j] does not seem identify the actual underlying value. Do you know of a way to resolve this issue? Commented Feb 17, 2020 at 5:00
  • right. == compares to see if they are the same object. The two strings, even if they the same value, are not the same object. That is why you would need userLocation.equals( board[i][j]) to compare string values. But see my answer on how not to need the string at all. Commented Feb 17, 2020 at 5:07

1 Answer 1

1

So the reason it will not compile is your = does not return a boolean expression. == would, but it's still not what you want, since you want to check if the String contents are the same, not if they're the same object, so use .equals.

But, no, I think you don't want to depend on strings to identify locations. What if you want to replace a card? And why look through everything when you need not?

Rather if i is some number between 1 and 20, identify the corresponding spot in the array by square[(i-1)/5][(i-1)%5]

That should bypass the issue you are having with matching strings.

So for example, your constructor becomes:

public GameBoard() {
square  = new String[4][5];
for (int i=1; i<=20;i++){
    square[(i-1)/5][(i-1)%5]=""+i;//initialize with 1 to 20 if you like
}

and userLocation is an int.

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

1 Comment

Thank you for the help Jeremy, I really appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.