1

I have a following problem - if else statements do not work in JSP, and to be honest I have no idea why. Basically I try to change the placeName depending on what number is stored in a string called place. After printing the values in the browser I can see the value is not changed. I am sure it is something simple but... Maybe some one had similar problem before?

<%
//requests the strings sent by previous page
String year = request.getParameter("year");
String place = request.getParameter("place");
out.print(year);
out.print(place);

String year2 = request.getParameter("year2");
String place2 = request.getParameter("place2");
//out.print(year2);
//out.print(place2);

if (place == "1")
{
placeName = "Belmullet";
}
else if (place == "2")
{
placeName = "Birr";
}
...more statements here...
else if (place == "15")
{
placeName = "Shannon airport";
};
%>
Dingredient
  • 2,191
  • 22
  • 47
Andrei Ivanov
  • 311
  • 2
  • 8
  • 19
  • 9
    You should use `.equals()` when comparing strings – Esailija Apr 17 '13 at 20:42
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Makoto Apr 17 '13 at 20:45
  • For people who answered. Perhaps you want to explain a little but about the differences between `==` and `.equals()` – Maroun Apr 17 '13 at 20:47
  • Remember that everything in java is a pointer, so when you use the == operator as found in c++, you are comparing the address. Not the value stored. – ArgumentNullException Apr 17 '13 at 20:47
  • Many thanks - it worked, I actually used that method before... The reason I did not think about it was that very similar code was used in Java Script - and it worked, so I thought it is something else – Andrei Ivanov Apr 17 '13 at 20:51

2 Answers2

4

change the if condition:

if (place == "1") {

}

by

if ("1".equals(place)) {

}

and the same way for the other if conditions.

This SO question may helps you to learn the difference between == and equals().

Community
  • 1
  • 1
Miguel Prz
  • 13,718
  • 29
  • 42
  • Many thanks - it worked, I actually used that method before... The reason I did not think about it was that very similar code was used in Java Script - and it worked, so I thought it is something else – Andrei Ivanov Apr 17 '13 at 20:49
2

It's because you're comparing Strings using ==. Instead, use the .equals() method.

The == operator tests to see if two object references refer to the exact same instance of an object.

The .equals() tests to see if the two objects being compared to each other are equivalent.

drew moore
  • 31,565
  • 17
  • 75
  • 112
  • 1
    If `place` is `null`, this throws `NullPointerException`. – Paul Vargas Apr 17 '13 at 20:45
  • if `place` is null in his original code, it throws a `NullPointerException` too. – drew moore Apr 17 '13 at 20:48
  • Many thanks - it worked, I actually used that method before... The reason I did not think about it was that very similar code was used in Java Script - and it worked, so I thought it is something else – Andrei Ivanov Apr 17 '13 at 20:55