PDA

View Full Version : boolean function always return as false


flint0131
03-07-2010, 11:11 AM
public boolean is_uNameRegistered ( String n ) {
FileInputStream fin;
StringTokenizer tokz;
String dbaseStr;
try {
String op = "";
fin = new FileInputStream("uName_db.txt");
dbaseStr = new DataInputStream(fin).readLine();
tokz = new StringTokenizer(dbaseStr);
while ( tokz.hasMoreTokens() ) {
op = tokz.nextToken();
if ( op.equals(n) ) {
return true;
}
}
fin.close();
return false;
} catch ( IOException e ) {
statusTxt.setText("Unable to read from databse.");
}

return false;
}
public boolean is_pWordRegistered ( String n ) {
FileInputStream fin;
StringTokenizer tokz;
String dBaseStr;
try {
String op = "";
fin = new FileInputStream("pWord_db.txt");
dBaseStr = new DataInputStream(fin).readLine();
tokz = new StringTokenizer(dBaseStr);
while ( tokz.hasMoreTokens() ) {
op = tokz.nextToken();
if ( op.equals(n) ) {
return true;
}
}
fin.close();
} catch ( IOException e ) {
statusTxt.setText("Unable to read from database.");
}

return false;
}

I'm trying to compare two strings, the input from the user and the one that is stored on my .txt file ( used as my database ). The problem is it always return as false.

I have this code :


if ( both functions above are true ) {
// some codes
} else {
// prompts incorrect password
}


it always "Incorrect Password".. DDD: somebody help? or suggest another solution?

flint0131
03-07-2010, 11:30 AM
uhh.. anyway, i found the solution. :]]

Shyama_Mukherji
03-08-2010, 05:07 AM
Refer to your code below :

tokz = new StringTokenizer(dbaseStr);
while ( tokz.hasMoreTokens() ) {
op = tokz.nextToken();
if ( op.equals(n) ) {
return true;
}
}
fin.close();
return false;
} catch ( IOException e ) {
statusTxt.setText("Unable to read from databse.");
}

return false;
}


In the piece of code above , delete both occurrances of the statement : return false


while ( tokz.hasMoreTokens() ) {
op = tokz.nextToken();
if ( op.equals(n) ) {
return true;
}
}
fin.close();
} catch ( IOException e ) {
statusTxt.setText("Unable to read from database.");
}

return false;
}

In the piece of code above delete the statement : return false