Home > Net >  If statement not working when trying to find entry in array
If statement not working when trying to find entry in array

Time:08-11

This is a shortened version of the code I'm having issues with.

This is an example of a possible input:

3
a b c
d e f
g h i

The 3 specifies the number of rows and columns, and the rest is what I want to be put into my array. My code then puts this input into an array:

String[][] arr = [[a, b, c], [d, e, f], [g, h, i]]

However, when I try use an if statement to check if the last entry is an i, the program doesn't recognise that the last entry is an i


if (arr[2][2] == "i"){
    System.out.println("There is an i");
    }
    else {
        System.out.print("No i found");
    }

In this example code, my program would say "No i found" even though there is an i at arr[2][2]

What could be the issue with my code?

CodePudding user response:

change if (arr[2][2] == "i") ==> if (arr[2][2].equals("i"))

String ref check will fail, use .equals method

  • Related