-2

I expect the output will be the string in the wordList when it matched the search before the for loop exits but it does not print out every time when the if statements met the condition.

search = "ABC"
wordList = [["ABC", "123"], ["ABC", "456"], ["DEF", "123"]]

public void biDi(String searchWord, String[][] wordList) {
    int start = 0; 
    int end = list.size ()-1;
    String search = searchWord;

    int path = 0;
    for (int i = 0; (i < (list.size ()/2)); i++) {
        if (search == wordList[start][0]) {
            System.out.println (wordList[start][1]);
        }
        if (search == wordList[end][0]) {
            System.out.println (wordList[end][1]);
        }

        start++;
        end--;
        path++;
    }

    System.out.println (path);

}

1 Answers1

1

You need to to use equals instead of ==, using == on string compare reference, not value.

if (search.equals(wordList[start][0])) {
    System.out.println(wordList[start][1]);
} 
if (search.equals(wordList[end][0])) {
    System.out.println(wordList[end][1]);
} 
Alex
  • 9,102
  • 3
  • 31
  • 35