Very simple if statement: baffling problem - need urgent help!
5 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, December 7, 2005 09:13 AM

OK, I have a simple if statement that's gone crazy: String threadstarter = request.getParameter("threadstarter"); if(threadstarter == "1"){ String sql2 = "UPDATE `"+table+"` SET `message` = "+thread+" WHERE `threadstarter` = -2 LIMIT 1"; sb.execute(sql2); }else{ out.println("threadstarter is:"+threadstarter); } This thing prints out: threadstarter is:1 every time! Does anyone jnow what the problem is? I'm brand new to JSP but as far as I can tell everything's   More>>

OK, I have a simple if statement that's gone crazy:



String threadstarter = request.getParameter("threadstarter");


if(threadstarter == "1"){

String sql2 = "UPDATE `"+table+"` SET `message` = "+thread+" WHERE `threadstarter` = -2 LIMIT 1";

sb.execute(sql2);



}else{

out.println("threadstarter is:"+threadstarter);

}



This thing prints out:



threadstarter is:1



every time! Does anyone jnow what the problem is? I'm brand new to JSP but as far as I can tell everything's fine!


Thanks in advance


-Ciaran


 

   <<Less

Re: Very simple if statement: baffling problem - need urgent help!

Posted By:   Anonymous  
Posted On:   Wednesday, December 7, 2005 10:05 AM

FANtastic! I got it working with that .equals thing. Thanks a million! By the way, I have an actionscript java background: if == is not for comparing than what is it used for? I've never had a problem with is in other languages.

Re: Very simple if statement: baffling problem - need urgent help!

Posted By:   Anonymous  
Posted On:   Wednesday, December 7, 2005 10:00 AM

Thanks everyone for the quick replies! I'll try them out and get back to you!
Cheers
-Ciaran

Re: Very simple if statement: baffling problem - need urgent help!

Posted By:   Anonymous  
Posted On:   Wednesday, December 7, 2005 09:58 AM

Bythe way, that SQL string is working fine, so there's no need to get distracted by it.

Re: Very simple if statement: baffling problem - need urgent help!

Posted By:   Axel_Richter  
Posted On:   Wednesday, December 7, 2005 09:38 AM

Hi,



>> String threadstarter = request.getParameter("threadstarter");

After this threadstarter is a String object.



>>if(threadstarter == "1")

Now you are testing if threadstarter is the same String object like the new string object "1". This returns false.



For comparing Strings you have to use the string comparison methods.



Try

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

greetings



Axel

Re: Very simple if statement: baffling problem - need urgent help!

Posted By:   WarnerJan_Veldhuis  
Posted On:   Wednesday, December 7, 2005 09:36 AM

By definition you should not compare Strings with ==. You should compare Strings with threadStarter.equals("1");
About | Sitemap | Contact