Having problems with String toLowerCase

572 Views Asked by At

I am having troubles with converting to lower case from a string. I need to be able to accept a multitude of case combinations to ensure that it works regardless of how the user inputs the 'priority' string.

Here is my enqueue method

public void enqueue(T element, String priority){
    priority = priority.toLowerCase();
    if (priority == "high"){
        prioritiesArr[0].add(prioritiesArr[0].count, element);
    }
    else if (priority == "medium"){
        prioritiesArr[1].add(prioritiesArr[1].count, element);
    }
    else if (priority == "low"){
        prioritiesArr[2].add(prioritiesArr[2].count, element);
    }
    else throw new IllegalArgumentException("Not a valid priority option");

    System.out.println("added " + element + " with priority " + priority );
}

And when the tester calls

test.enqueue(1, "High");

I get the IllegalArgumentException. The problem is that when I did some print debugging, the string was, in fact, lowercase, but when I go to compare it to the lowercase version in enqueue it appears that it does not have the same value.

Is there some kind of comparison problem? Please give me a hand! Thanks

1

There are 1 best solutions below

1
On BEST ANSWER

use String.equals() for comparison using Strings.