User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 392,037 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,285 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 2683 | Replies: 4 | Solved
Reply
Join Date: Jan 2008
Posts: 27
Reputation: eddy556 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
eddy556 eddy556 is offline Offline
Light Poster

JSP if statement

  #1  
Feb 11th, 2008
Hey, I'm having a problem with an if statement. I want to check if variable key and id are the same. They are both strings and when I print them to screen they certainly look the same. However when using them within an if statement it always returns false. Here is my code, I have commented where the statement is not reverting true when it should. Thanks for your help:

  1.  
  2. <%@ page import="java.sql.*" %>
  3. <%@ page import="java.util.*" %>
  4. <%@ include file="connection.jsp" %>
  5.  
  6.  
  7. <html>
  8. <head>
  9. <title>Web hosting FAQ</title>
  10. </head>
  11.  
  12. <body>
  13.  
  14. <center><h1>Customer area</h1></center>
  15. Frequently asked questions about web hosting: <br />
  16. <%
  17.  
  18. String key = null;
  19. key = request.getParameter("question");
  20.  
  21. String id = null;
  22. String Question = null;
  23. String Answer = null;
  24. String Updated = null;
  25. String Author = null;
  26.  
  27.  
  28. ResultSet columns1 = statement.executeQuery("SELECT * FROM Questions");
  29.  
  30. while(columns1.next())
  31. {
  32. id = columns1.getString("ID");
  33. Question = columns1.getString("Question");
  34. Answer = columns1.getString("Answer");
  35. Updated = columns1.getString("Timestamp");
  36. Author = columns1.getString("Author");
  37.  
  38. if (id == key)
  39. {
  40. out.print("This statement should be true!);
  41.  
  42. }
  43.  
  44. %>
  45. <a href="custarea.jsp?question=<%=id %>"><%=Question %></a> <br />
  46. <%
  47.  
  48.  
  49. } %>
  50.  
  51. </body>
  52. </html>
  53.  
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,775
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: JSP if statement

  #2  
Feb 11th, 2008
The == operator in case of references compares the references and not the values to which they refer. Since both id and key don't refer to the same string instance in memory, it always returns false. What you need here is to use the equals() method to do the same.

But seriously, this is the very basic of Java programming every programmer out there ought to know. I would recommend you go through the basic Java programming tutorials on the official Sun site before diving head first in J2EE.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jan 2008
Posts: 27
Reputation: eddy556 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
eddy556 eddy556 is offline Offline
Light Poster

Re: JSP if statement

  #3  
Feb 11th, 2008
Okay thanks for you're help but I've just come from a C# background (hence I didn't know the different between == and equals() and I've only been learning the language for a few weeks
Reply With Quote  
Join Date: Jan 2008
Posts: 32
Reputation: electron33 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
electron33 electron33 is offline Offline
Light Poster

Re: JSP if statement

  #4  
Feb 14th, 2008
Try this. It compares to strings.

if (id.equals(key))

{

out.print("This statement should be true!);

 

}
Reply With Quote  
Join Date: Jan 2008
Posts: 27
Reputation: eddy556 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
eddy556 eddy556 is offline Offline
Light Poster

Re: JSP if statement

  #5  
Feb 15th, 2008
Yea thanks I figured it out from ur previous post, thanks 4 ur help
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JSP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 11:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC