0

I have a helper method that I am calling from another method in the same class. When I test it from main, it works fine. But as soon as I use it in the other class, it doesn't work at all. I cannot figure out what is wrong. This is the helper method:

private boolean checkStack(Stack<String> stack,String check) { 
    System.out.println(stack);
    System.out.println(check);
    Stack<String> jump = new Stack<String>();
    int count = 0;
    String temp = "";
    while (!stack.empty()) {
        temp = stack.pop();
        if (check == temp) {
            count++;
        }
        jump.push(temp);
    }
    while (!jump.empty()) {
        temp = jump.pop();
        stack.push(temp);
    }
    System.out.println(count);
    if (count != 0) {
        return true;
    } else {
        return false;
    }

}

I will test it from main like so:

    pathSoFar.push("00");
    pathSoFar.push("01");
    pathSoFar.push("20");
    pathSoFar.push("23");
    System.out.println(pathSoFar);
    String checkfor = "20";
    System.out.println(test01.checkStack(pathSoFar,checkfor));

But it wont work when I call it from another method:

            for (String n : possibleSpots) {
                System.out.println();
                String check = n;
                if (!checkStack(pathSoFar, n)) {
                    pathSoFar.push(n);
                    String x = ""+n.charAt(0);
                    String y = ""+n.charAt(1);
                    int nextRow = Integer.parseInt(x);
                    int nextCol = Integer.parseInt(y);
                    System.out.println(nextRow + "" + nextCol + " = next move.");
                    if (findPath(wordToFind, pathSoFar, nextRow, nextCol)) {
                        return true;
                    }
                } else{}
            }

This is the method header if that helps:

private boolean findPath(String wordToFind, Stack<String> pathSoFar, int row, int col) {
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    And could you define what "doesn't work at all" means? I'm sure at least the prinltn's happen. – markspace Mar 07 '18 at 03:33
  • Yeah sorry, the checkStack method returns false, even if it should be true. When I tested inside checkStack (where you see println(count)) the count hasnt incremented as it should – Joel Neuman Mar 07 '18 at 04:21
  • What is possibleSpots list? Can you share the code how it is populated? – vbbharath Mar 07 '18 at 04:46

1 Answers1

0

possibleSpots can contain either String literals or String objects.

The problem lies in the following line

if (check == temp)

Change it to

if (check.equals(temp))

String matching is done with == which will work only for String literals. That is the reason why it worked for you in one case and it does not work in another case.

To know the difference check the below link:

What is the difference between == vs equals() in Java?

vbbharath
  • 127
  • 4
  • Valid point, but is this also the reason for why it works from main but not from different methods? –  Mar 07 '18 at 11:37
  • @burrito77 As per the posted main method code, only String literals are added in the 'pathSoFar' stack. Whereas in the other method, if the 'possibleSpots' is filled with String literals then checkStack() method would have returned true. But since it has String objects '==' comparison fails and the method returns false. – vbbharath Mar 07 '18 at 12:02
  • Yeah, I would add that to the answer to explain why it works on main but not on the other, as that was the question. –  Mar 07 '18 at 12:48