I'm making a an application and I want the application to automatically log in from a text file if the user has already logged in. Currently, in the text file i have "alex|ppp" which matches the database entry. The following method is called 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 have double checked the values for user and pass and they are "alex" and "ppp" which is expected. Next InitialStuff is called, this 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 occurs once i call the db.tryLogin() which 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 have checked to see that dbPass comes back from the database as "ppp" which matches toonPass yet it will skip over the return true and return false instead.
If it helps, this is the information eclipse gives me about the two
toonPass "ppp" (id=830041185816)
count 3
hashCode 0
offset 5
value (id=830041185744)
[0] a
[1] l
[2] e
[3] x
[4] |
[5] p
[6] p
[7] p
dbPass "ppp" (id=830041708816)
count 3
hashCode 0
offset 0
value (id=830041709136)
[0] p
[1] p
[2] p
Pleaes note that i have also tried passing in "ppp" to the tryLogin() method without taking it as a substring in case that had something to do with the problem and the outcome is the same.
EDIT: I solved the problem...sorta. I just stopped using the .equals() method and instead am using a for loop that compares the characters in each string to one another