user1873875
user1873875

Reputation:

Java Showing correct Incorrect

I am having this problem which should not be a problem

i have written this code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author ZEESHAN
 */
public class TestApps 
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        // TODO code application logic here
        Connection con;
        // Statement stmt;
        ResultSet rs;
        String str,str2,str3;
        try 
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:TestApp");

            Statement stmt =  con.createStatement();
            System.out.println("Connected");

            rs=stmt.executeQuery("SELECT Emp_Name,Emp_LastName FROM Table_1 WHERE Emp_Name = 'zeeshan' ");

            while(rs.next()) 
            {
                str = rs.getString("Emp_Name");
                str2 = rs.getString("Emp_LastName");
                String name,pass;    

                Scanner input = new Scanner(System.in);
                name = input.nextLine();
                pass = input.nextLine();

                if(name.equals(str) && pass.equals(str2) )
                {
                    System.out.println("CORRECT");
                }
                else
                {
                    System.out.println("INCORRECT");
                    System.out.println(""+name);
                    System.out.println(""+pass);
                    System.out.println(""+str);
                    System.out.println(""+str2);
                }
            }

            //  String name,pass;    

            /*   
            if(name.equals(str) && pass.equals(str2) )
            {
                System.out.println("Correct");
            }
            else
            {
                System.out.println("Incorrect");
            }
            */
        }
        catch (Exception ex)
        {
            System.out.println("Not connected");
            ex.printStackTrace();
        }
    }
}

Everything works fine, i.e. database and all. I giving the same answer as the database string, but I get this result which is not fine and also the result says all strings are the same answer, but why I still don't get in the true condition I am getting the false condition my answer in console.

RESULT:
 INCORRECT
 zeeshan
 ismail
 zeeshan   
 ismail  

Upvotes: 1

Views: 118

Answers (4)

subramanya
subramanya

Reputation: 26

Yes it is the leading and trailing spaces that is causing this error.

You can also do trim on "name" and "pass" before comparing these with str and str1.

name= name.trim();
pass=pass.trim();

Upvotes: 0

Chan
Chan

Reputation: 2641

Best is to do it this way -

str = str.toUpperCase().trim();
str2 = str2.toUpperCase().trim();

and compare

if(str.equals(str2)){}

If you are using String comparison be sure to

  • Bring both strings to the same case
  • Trim both strings

Upvotes: 0

Ahmet Karakaya
Ahmet Karakaya

Reputation: 10139

You should try to use trim() method.

Upvotes: 0

Stanley
Stanley

Reputation: 5137

There are trailing spaces in the value of str and str2. You have to trim to values before comparing the string.

str = str.trim();
str2 = str2.trim();

Upvotes: 2

Related Questions