0

Cannot understand why following code doesn't work properly. Firstly, system getting input from the user. Then system reads all data from .txt file and compare with the user input. But the system never finds similar username and password.

The idea is to create simple login that based on the stored username and password in .txt file. Could someone help?

private static void login() { String record = null; FileReader in = null; try { in = new FileReader("adminlogin.txt"); BufferedReader br = new BufferedReader(in); Scanner keyboard = new Scanner(System.in); System.out.print("Username: "); String user = keyboard.nextLine(); System.out.print("Password: "); String pass = keyboard.nextLine(); while ((record = br.readLine()) !=null) { if (user.equals(record) && pass.equals(record)) { Mainemenu menu = new Mainemenu(); menu.AdminMenu(); } else { System.out.println("________----Error----________\n press 'Enter' to continue..."); keyboard.nextLine(); checkInput(); } } } catch (IOException e) { e.getCause(); } } 
1
  • 1
    Use your debugger to step through the code line by line, and it should become obvious. Hint: you print an error as soon as you find one line of the file that doesn't match with the user name. You want to print an error if none of the lines match. That can only be done after the loop. You must also break out of the loop if you found the user. Advice: start using methods, and create one with this signature: boolean existsLineEqualTo(BufferedReader reader, String userName). Commented Dec 25, 2016 at 23:03

1 Answer 1

2

Your problem is the loop and its comparison:

while ((record = br.readLine()) !=null) { if (user.equals(record) && pass.equals(record)) { //... } //... } 

You read a whole line from your file, which is in record, but then you compare both user and pass with this line. This will never work, except user is equal to pass.

Either you have stored the user name and password in a line in your file - then you have to split the line into user name and password - or you have the name and password stored in two separate lines - then you need to reads in the loop for each user.

Moreover, you do throw an error after you checked only the first user and you do not exit the loop, if you actually found the user.

Solutions

I suppose your records in the file are like "username password", then do:

Mainemenu menu = null; while ((record = br.readLine()) !=null) { // Split line by a whitespace character // split[0] <- username // split[1] <- password String[] split = record.split("\\s"); if (user.equals(split[0]) && pass.equals(split[1])) { menu = new Mainemenu(); menu.AdminMenu(); // You found the user, exit the loop break; } // Delete else branch } if (menu == null) { // User not found } 

Of course you can use any other delimiter character or sequence for your records by adopting the delimiter string in split.

Sign up to request clarification or add additional context in comments.

3 Comments

Updated solution to other flaws in if-else branches.
Thanks, but supposed to be String[] split = record.split("\\s");
@BogdanSkripnik Thanks, my mistake. Edited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.