• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IF condition

 
Hak Khan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have retrieved a column value from a table in my query within my JSP program. The column value contains "Y". Of course without double codes.

In my program, I want to assing a radio button value based on column value whether its Y or N. Looks like my if condition is not working properly. I have tried to put some println messages, its always printing from else part.


Here is my code:

if (resultSet.getString(2) == "Y")
{
out.println("<td><input type=\"radio\" name=rsincrmt checked>ON");
out.println("<input type=\"radio\" name=rsincrmt >OFF</td>");
}
else
{
out.println("<td><input type=\"radio\" name=rsincrmt>ON");
out.println("<input type=\"radio\" name=rsincrmt checked>OFF</td>");
}

Any help would be highly appreciated

Thanks in advance
 
Nikhil Menon
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hak Khan,

You just made a minor mistake.
Dont use == operator for comparing value equality in Strings. (It actually compares references). Use equals(String str) method instead. That will work fine, and remember if you need to ignore the case when comparing better use equalsIgnoreCase(String str) instead. Thats all.
See api http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Regards,
Nikhil Kanjulli Menon.
 
Hak Khan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nikhil,

First of all, thank you so much for your reply. I did try equals method but doesn't work. I did something like this. Please tell me if I'm doing anything wrong here.

if (resultSet.getString(2).equalsIgnoreCase("y"))
{
out.println("<td><input type=\"radio\" name=\"rsrec1\" checked>ON");
out.println("<input type=\"radio\" name=\"rsrec1\" >OFF</td>");
}
else
{
out.println("<td><input type=\"radio\" name=\"rsrec1\" >ON");
out.println("<input type=\"radio\" name=\"rsrec1\" checked>OFF</td>");
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is clearly not a JSP issue per se, so I've moved it along to the JDBC forum.
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you try printing the value of resultSet.getString(2)before the if statement to make sure that the value retrieved is "Y" without the quotes of course; this atleast helps with debugging. Programming could be pain sometimes.
 
alan do
Ranch Hand
Posts: 354
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the DB field length > 1 and type is CHAR (instead of VARCHAR), depending on the DB, there will be one or several " " behind the "Y". you need to do resultSet.getString(2).trim().equalsIgnoreCase("Y")
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic