•
•
•
•
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
![]() |
•
•
Join Date: Jan 2008
Posts: 27
Reputation:
Rep Power: 1
Solved Threads: 0
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:
java Syntax (Toggle Plain Text)
<%@ page import="java.sql.*" %> <%@ page import="java.util.*" %> <%@ include file="connection.jsp" %> <html> <head> <title>Web hosting FAQ</title> </head> <body> <center><h1>Customer area</h1></center> Frequently asked questions about web hosting: <br /> <% String key = null; key = request.getParameter("question"); String id = null; String Question = null; String Answer = null; String Updated = null; String Author = null; ResultSet columns1 = statement.executeQuery("SELECT * FROM Questions"); while(columns1.next()) { id = columns1.getString("ID"); Question = columns1.getString("Question"); Answer = columns1.getString("Answer"); Updated = columns1.getString("Timestamp"); Author = columns1.getString("Author"); if (id == key) { out.print("This statement should be true!); } %> <a href="custarea.jsp?question=<%=id %>"><%=Question %></a> <br /> <% } %> </body> </html>
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.
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."
"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."
•
•
Join Date: Jan 2008
Posts: 32
Reputation:
Rep Power: 1
Solved Threads: 4
Try this. It compares to strings.
if (id.equals(key))
{
out.print("This statement should be true!);
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JSP Marketplace
Similar Threads
- Please help with JSP mysql update query (JSP)
- jsp and mysql (JSP)
- Move out java code from jsp (Java)
- JSP codes seems not executed correctly (JSP)
- jsp - oracle (JSP)
- JSP connection to database (JSP)
- change statement (JSP)
Other Threads in the JSP Forum
- Previous Thread: autosuggest textbox with java from database
- Next Thread: Dynamic Page Generation in JSP?



Linear Mode