Problem with if statement...

I have a form with a hidden field named fsearch

On the next page I use:

String search = (String) request.getParameter("fsearch");

The value if search coming through is "location".

Now I have this if but it never enters the loop...why?

if (search == "location")

{

out.println("<center>Choose a location:<P>");

// generate query

String Query = "SELECT DISTINCT city FROM resumeHolders ORDER BY city";

// get result

ResultSet SQLResult = SQLStatement.executeQuery(Query);

while(SQLResult.next())

{

//Name= SQLResult.getString("firstName");

City= SQLResult.getString("city");

out.println("<a href=\"view_resumes.jsp?city=" + City + "\">" + City + "</a>
");

}

// close connection

SQLResult.close();

SQLStatement.close();

Conn.close();

}

[1059 byte] By [hyrum14] at [2007-9-26 1:59:49]
# 1
Use 'if (search.equals("location"))'.Using '==' tests for equality between the reference to 'search' and the reference to the literal "location"
eashanno at 2007-6-29 3:19:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi,You cannot use == for stringsUse: if (search.equals("location")){//do something}The above will work
adamrau at 2007-6-29 3:19:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

HI !!

First of all try printing the string on the HTML page in the browser itself : i.e out.println(search)

You will then see if the String variable "search" is being assigned the value of the request.getParameter("fsearch");

If you see "location" on the screen then , the next step would be to do a "location".equalsIgnoreCase(search.trim())

This will narrow the scope of errors. If nothing else works , try instantiating the variable search as a String Object with :

String search=new String();

if (request.getParameter("fsearch") == null || "".equals(request.getParameter("fsearch")) {

search=null;

}

else {

search=(String)request.getParameter("fsearch");

}

Hope all this helps....

Regards

Chandu

chandu_9 at 2007-6-29 3:19:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thanks a lot everyone, a new to Java error, appreciate everyones help!
hyrum14 at 2007-6-29 3:19:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
You can also use if(string1.compareTo("string2")==0){//will be executed if string1 and string2 are equal } else{ //will be executed if string1 != string2}
p_t_hari_das at 2007-6-29 3:19:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...