-1

Alright the input is a string of numbers and operators such as, + 2 3 * 4. I want to split the string up into an ArrayList object, with each character in the string getting it's own index. Here's what I have;

String current;
String s = "+ 2 3 * 4";
List <String> splitter = new ArrayList<String>(Arrays.asList(s.split("\\s+")));
current = splitter.remove(0);

if (current == "+"){
   //do stuff }
else if (current = "-") {
   //do stuff } 
....

The if statements aren't being evaluated as true for some reason, am I missing something subtle or am I completely wrong here?

Thanks for the help.

Talen Kylon
  • 1,908
  • 7
  • 32
  • 60
  • 2
    you cannot compare a `String` using the `==` operator, you need to use `.equals()` – iberbeu Mar 26 '13 at 23:01
  • yes, and (current = "-") as an condition shouldnt even compile, because it is assigment, not comparation (use == to compare and .equals() to compare objects) – kajacx Mar 26 '13 at 23:03

1 Answers1

8

In the if, you are using == to compare strings; use String#equals instead.

In the else, you are using the assignment operator = to compare strings; use String#equals instead.

The == operator compares the two string references to see if the refer to the same object, which is not what you want.

rgettman
  • 176,041
  • 30
  • 275
  • 357