After being marked as duplicate:
Ok I see that now this is a duplicate, and I apologize for a seemingly silly question. It's just from my perspective it was difficult to realise what to "search" online to solve this problem because I didn't know using ==
with strings in this manner was a problem to begin with! Even so, I'm still wondering why is using the test
array still work in the for
loop at the bottom of the code despite it checking with ==
and not .equals()
but it doesn't work for the array created with stored Scanner
input? It's because one of these methods still worked in finding p
with ==
and not .equals()
that made me think it was the method of storing the array that caused the problem and look in the wrong places for answers.
Problem:
I'm trying to make a simple AI for a problem I'm working on in Java, and part of it requires storing user input with the Java Scanner
library in order to make an array of strings that represents a game board or "grid". I decided to store the input of this array
"grid" one by one, asking the user to enter element for the current array position. The array is a 2-dimensional array and the input is stored one by one via two for
loops one nested in the other as you can see near the start of the code I have provided below. I made a method called displayGrid
that prints out the contents of 2-dimensional array in the form of a grid and it seems to take in the input correctly and the board displays correctly.
However when I try to search this "grid" after storing it with input (by using for
loops as you can see near the end of the code snippet) the if
statement there can't seem to verify as true
even when I have stored the letter "p" in the first part of running the program when the Scanner is storing input into the array, and the displayGrid
method actually prints out the array correctly showing there is in fact a "p" in the array. In any case, this part of the code is supposed to check if there is a "p" in the array and if there is return it's column position which I tried doing but it always returns 0 since it cannot verify that if
statement as true
.
The strangest thing though is that if I make a test array that is composed of strings as you can see at the very start of the code, and try the loop at the bottom of the code on this test
array it does find "p" and print it's column value.
So are there any ideas why the loops at the bottom cannot find the "p" value of an array created from the scanner inputs near the start of the code - even when my displayGrid
method shows that this array does contain a "p"? Thanks in advance!
Note: I haven't included rest of the code because it was largely irrelevant, but I can post it if it's really needed.
public static void main(String[] args) {
// tests
String[][] test = {{"-", "p", "-"},{"-", "-", "-"},{"-", "-", "-"}};
// running main program
Scanner in = new Scanner(System.in);
int n = 0, boardSize;
boardSize = n*n;
do {
System.out.print("Enter n dimension of your game grid (for an nxn sized board): ");
n = in.nextInt();
} while(n < 3 || n > 100 || n % 2 == 0);
String grid[][] = new String[n][n];
System.out.println("Please enter the contents of your array now.");
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
System.out.print("Enter element at grid location: [" + row + "] [" + col + "]: ");
String temp;
temp = in.next();
grid[row][col] = temp;
}
}
// display grid for testing
displayGrid(grid, n);
// test
for(int row=0; row<grid.length; row++) {
for(int col=0; col<grid.length; col++) {
if (grid[row][col] == "p") {
System.out.println(col);
}
}
}