Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file for authentication help

Status
Not open for further replies.

aagustin

Programmer
Jul 23, 2001
12
US
Howdy,

I'm trying to write a method which needs to read a text file formatted:

user:password:product1:product2

I have it working only if there's one line in the text file, more than one, it crashes. Here's my code, any clue to what I need to do to get it working?

private boolean validateUser(String id, String password) {

String record = null;
int TokCounter=0;
int TotLen = 0;
int LookLen = 0;
boolean matching = false;
String LookFor = null;

try {

FileReader fr = new FileReader("C:/password.txt");
BufferedReader br = new BufferedReader(fr);
while ((record = br.readLine()) != null) {

LookFor= id + ":" + password + ":";
record = new String();
record = br.readLine();

if (record.startsWith (LookFor))
TotLen = record.length();
LookLen = LookFor.length();
record = record.substring(LookLen, TotLen);
StringTokenizer tok = new StringTokenizer(record, ":");

while (tok.hasMoreTokens()){
String token = tok.nextToken();
TokCounter = TokCounter + 1;
if (TokCounter==1)
CompanyName= token;
if (TokCounter==2)
Prod1= token;
if (TokCounter==3)
Prod2= token;
matching = true;
}
} catch (IOException e) {
matching = false;
}
return matching;
}


Thanks for any help.
 
Hi,
I went through ur code and found that if u put ur TotLen and LookLen and the substring operation within the if statements limits ur program will work fine. Heres what u need to modify.

if (record.startsWith (LookFor))
{
TotLen = record.length();
LookLen = LookFor.length();
record = record.substring(LookLen);
}

Hope this helps,
Reddy316
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top