0

So I am writing a program that iterates through a text file. The format of the text file is a username and password on the same line separated by a space. If a user enters a username that exists, the program then prompts for a password. If that user and password entered already exist in the text file, then a message will say the user exists already. If not, then the user and their password will be appended to the text file. The issue I am having is I am not sure how to check the specific lines in the text file that has the username matches as opposed to every line in the text file. Is this possible?

public static void main(String[] args) throws IOException {

    FileWriter fwriter = new FileWriter("UserData.txt", true);
    PrintWriter outputWriter = new PrintWriter(fwriter);

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    boolean exists = false;

    // Open the file.
    File file = new File("UserData.txt");
    Scanner inputFile = new Scanner(file);

    System.out.println("Please enter a username");
    String username = keyboard.nextLine();

    while (inputFile.hasNext()) {
        if (username == inputFile.next()) {
            exists = true;

        }

            while (exists = true) {
                System.out.println("Please enter a password");
                String password = keyboard.nextLine();
                String hashedPassword = md5(password);

                if (hashedPassword == inputFile.next()) {
                    System.out.println("This user exists already!");
                }

                else if (hashedPassword != inputFile.nextLine()){
                    outputWriter.println(username + " " + md5(password));
                    System.out.println("This user has been added!");
                }

            }

            while (exists = false) {
                System.out.println("Please enter a password");
                String password = keyboard.nextLine();
                outputWriter.println(username + " " + md5(password));
                System.out.println("User has been added to the system");

            }
        }


    // Close the file.
    inputFile.close();

}
  • 2
    Use Scanner.nextLine() – Sash Sinha Dec 05 '16 at 02:05
  • as opposed to inputFile.nextLine()? – atrophydefined Dec 05 '16 at 02:07
  • That's what @atrophydefined meant. Use your Scanner object which you name inputFile. Another problem: Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Dec 05 '16 at 02:12
  • What will happen if the user existed but the password is not the one that was entered? – Lord_PedantenStein Dec 05 '16 at 02:36
  • OK found it in the code - you want to add him then i guess - then a user might have mutliple passwords!? o_O – Lord_PedantenStein Dec 05 '16 at 02:42
  • forgot to add this is assuming everyone will enter a unique username – atrophydefined Dec 05 '16 at 02:44
  • i made a working clean code Version of this but now the question is closed, not so nice :/ - so you need just one while loop and differentiate the cases if username matches input or not using if and else and go from there. – Lord_PedantenStein Dec 05 '16 at 03:02
  • And you do not need the exists variable if you check if a username equals the files content. And `// use buffer for better performance on large files FileInputStream fin = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fin));` then you go `while ((dummy = br.readLine()) != null) {` as the while loop – Lord_PedantenStein Dec 05 '16 at 03:09

0 Answers0