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();
}