Results 1 to 4 of 4
  1. #1
    emp
    emp is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default String compare issues?

    I am trying to compare two strings using the .equals() option. I am obtaining a value from a JDBC connection and trying to validate it against a user entered string, however it always fails. I have the following code -

    String search = request.getParameter("usercn"); //get usercn from HTML form
    while (rs.next()) {
    String result = rs.getString(1); //get the current loop value
    out.println("Current node:" + result + "<br>"); //print out current value
    out.println("search:" + search + "<br>"); //print out user string
    String r1 = new String (result);
    String r2 = new String (search);
    out.println("r1:" + r1 + "<br>"); // print out r1 value
    out.println("r2:" + r2 + "<br>"); //print out r2 value
    out.println(r1.equalsIgnoreCase(r2)); //debug - print out true/false
    if (r1.equalsIgnoreCase(r2)){
    String match = "true";
    out.println("match" + r1);}


    what I see is the following -

    r1:root
    r2:root
    false

    even though the strings appear the same, the comparison is always false.

    Any thoughts??

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default

    Hi,
    One small suggestion.u trim that string value and try like this below
    r1.trim().equalsIgnoreCase(r2.trim())

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default

    String search ="";
    if((request.getParameter("usercn") !=null) ||( request.getParameter("usercn").length() != 0) ||(request.getParameter("usercn") != ""))
    {
    search = request.getParameter("usercn");
    }

    while (rs.next()) {
    //Here dont retreive via numbers.Readability sake retreive via columnName
    String result = rs.getString(1); //get the current loop value

    out.println("Current node:" + result + "<br>"); //print out current value
    out.println("search:" + search + "<br>"); //print out user string
    //The below lines are not needed.Already u are having string
    //String r1 = new String (result);
    //String r2 = new String (search);

    out.println(result.trim().equalsIgnoreCase(search. trim())); //debug - print out true/false
    if (r1.equalsIgnoreCase(r2)){
    String match = "true";
    out.println("match" + r1);}

  4. #4
    emp
    emp is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 08:06 AM
  2. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 AM
  3. String Compare not working
    By Revelation in forum New To Java
    Replies: 3
    Last Post: 06-30-2007, 06:43 PM
  4. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 05:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •