Java .equals() returns false when strings are equal

Java .equals() returns false when strings are equal … here is a solution to the problem.

Java .equals() returns false when strings are equal

I’m making an app where if the user is already logged in, I want the app to automatically log in from a text file. Currently, I have “alex|ppp” in the text file that matches the database entry.
Call the following method first

private void rememberedLogIn(){
    String filename = "UserInfo.txt";
    String info = "";
    String user = "";
    String pass = "";

try{
        FileInputStream fIn = openFileInput(filename);
        BufferedReader r = new BufferedReader(new InputStreamReader(fIn));
        info = r.readLine();

}catch(IOException e){
        e.printStackTrace(System.err);
    }

for(int i =0; i < info.length(); i++){
            if(info.charAt(i) == '|' ) {
                user = info.substring(0,i);
                pass = info.substring(i+1);
                GlobalVar.loggedIn= true;
                break;
            }
        }
        new InitialStuff().execute(user,pass);
}

I’ve double-checked the values for user and pass, which are the expected “alex” and “ppp”. Next call InitialStuff, which is the relevant code:

public class InitialStuff extends AsyncTask<String, Void, Toon>{
    int prog = 0;
    @Override
    protected Toon doInBackground(String... params) {
        android.os.Debug.waitForDebugger();
        Toon toon = null;
        Database db = new Database();
        db.establishConnection();
        if(db.tryLogIn(params[0], params[1])){
            prog = 2;
            publishProgress();
            toon = db.getToonFromDB(params[0]);
            prog = 4;
        }else prog = 3;
        publishProgress();
        return toon;
    }}

The problem happened when I called db.tryLogin() and it looks like this

public boolean tryLogIn(String toonName, String toonPass){
    try{
        while(!connected) establishConnection();
        String sqlQuery = "SELECT Password FROM Toons WHERE Name LIKE '" + toonName+"';";
        Statement stmt = con.createStatement();
        ResultSet rSet = stmt.executeQuery(sqlQuery);
        if(rSet.next()){
            String dbPass = rSet.getString(1).trim();
            if(dbPass.equals(toonPass)) //PROBLEM OCCURS HERE
                return true;
        }
    }
    catch(Exception e){ }
    return false;
}

I’ve checked that dbPass returns “ppp” from the database as matching toonPass, but it will skip returning true and returning false.

If it helps, here’s what Eclipse gave me about both

toonPass “ppp”(id=830041185816)
Number 3
Hash code 0
Offset 5
Value (id=830041185744).
[0] A
[1] l
[2] Electronics
[3] x
[4] |
[5] p
[6] p
[7]p

dbPass “ppp”(id=830041708816)
Number 3
Hash code 0
Offset 0
Value (id=830041709136).
[0] p
[1] p
[2]

Note that I also tried passing “ppp” to the tryLogin() method without taking it as a substring, in case it is related to the problem and the result is the same.

Edit: I solved this problem… Somewhat. I just stopped using the .equals() method and instead used a for loop to compare the characters in each string

Solution

A hint!
If you are playing with String class methods, for example. .compare(), .equals(), etc. Remember the character set encoding! In particular, make sure to match the encoding of the IDE, project files, resources, and databases when you load/read data as strings from external sources

Related Problems and Solutions