0

I am experiencing some strange issues with request.getParameter("param") in Java. I am trying to test the returned value to set a boolean, nothing fancy. :)

String param = String.valueOf(request.getParameter("param"));
boolean paramIsAll = false;
if (param == "all"){paramIsAll = true;}

System.out.println("-"+ param +"-"+ String.valueOf(paramIsAll));

My output is: -all-false

???

Kermit
  • 2,865
  • 3
  • 30
  • 53
  • See this answer: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – stridecolossus Apr 24 '14 at 15:41
  • 1
    Also, an `if` statement that then performs a single line of code that returns or sets a boolean generally indicates poor coding. You can do that in a single line: `boolean paramIsAll = "all".equals(request.getParameter("param"));`. The `getParameter` method also already returns a string, there's no need for the `String.valueOf` call. – Anthony Grist Apr 24 '14 at 15:44
  • Thank you for the feedback! I "simplified" the code before posting and have since removed the single line if statement and "String.valueOf" call. – Kermit Apr 26 '14 at 07:48

2 Answers2

2

try with if (param.equals("all")){paramIsAll = true;}

while comparing 2 strings use String#equals()

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

Try: If(param.equalsIgnoreCase("all")){paramIsAll = true;}

FCoelho
  • 165
  • 1
  • 4
  • 11