dsociety dsociety - 1 month ago 14
Java Question

Loop through the file as long as the userinput is same as string in the file

Hey I'm working on a Bank account application and i'm working on the login at the moment. I want the Programm to go through the file and check if there is already a username same as the one the user wants, if there is one it should go back and ask for a new username again. I've tried many different ways but i couldn't find one that works. I hope someone can help me. :-)

String file = "C:\\Users\\dsociety\\IdeaProjects\\bankaccount\\logins.txt";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader on = new BufferedReader(new InputStreamReader(System.in));
String username;
String pw;


Boolean exists = false;
Scanner scanner = null;
try {
scanner = new Scanner(new File(file));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}


System.out.println("Please enter a username");
username = in.readLine();

BufferedReader bru = new BufferedReader(new FileReader(file));

String line;

try {
while (exists == true) {
while ((line = bru.readLine()) != null) {
while (scanner.hasNextLine()) {
String[] userAndPw = scanner.nextLine().split(":");
String user = userAndPw[0];

if (user.equals(username)) {
System.out.println("There is already a User with that username, please try a other username");
exists = false;
}
else {
exists = true;
}
}
}
}

bru.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


System.out.println("Please enter a password");
pw = on.readLine();

Answer

I looked through your code and, as someone else noticed, you can't enter the first while loop because of the exists variable being false. And I think you messed the order of the whiles a little bit. You definitely don't need a Scanner and a BufferedReader (one of them is enough) for reading the file, just as how you don't need two different BufferedReaders to read from the system input (again, one is sufficient). I modified your code a little bit and added some comments and I hope it works for you now:

    String file = "C:\\logins.txt";
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String username;
    String pw;

    System.out.println("Please enter a username");

    BufferedReader bru = new BufferedReader(new FileReader(file));
    // we will keep the users here instead of reading the file everytime a username is entered
    List<String> users = new ArrayList<>();

    Boolean exists = null;
    String line;

    // populate the existing users list by reading all the lines in the file
    while ((line = bru.readLine()) != null) {
        users.add(line.split(":")[0]);
    }

    bru.close();

    // we can enter the while loop if we are asking for the username for the first time
    // or if the username entered already exists
    while (exists == null || exists == true) {
        // we read the username from the input
        username = in.readLine();
        // we suppose it doesn't already exist
        exists = false;
        // we look at every existing user
        for (String user : users) {
            /*
             * if the names are the same, we don't need to look further, we know the username
             * exists and so we will break from the "for" loop and ask for a username once again
             * (side note: I used replaceAll in order to remove all invisible unicode characters
             * that might make two strings unequal that would otherwise be identical)
             */
            if (user.replaceAll("\\P{Print}", "").equals(username.replaceAll("\\P{Print}", ""))) {
                System.out.println("There is already a User with that username, please try a other username");
                exists = true;
                break;
            }
        }

    }

    System.out.println("Please enter a password");
    pw = in.readLine();