The loop is working fine except no matter what you put into txtUsername
and txtPassword
and I click the Login button, I get the message box saying invalid password try again. It is not crashing, just saying the password and username are always incorrect. Any help would be greatly appreciated!
btnLogin.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
File loginF = new File("users.txt");
//userTextField.getText();
//pwBox.getText();
//Scanner fileScan = null;
try {
Scanner fileScan = new Scanner(loginF);
fileScan = new Scanner(new File("users.txt"));
fileScan.useDelimiter(",");
boolean found = false;
while (fileScan.hasNextLine()) {
String input = fileScan.nextLine();
String Username = input.substring(0, input.indexOf(' '));
String Password = input.substring(input.indexOf(' '), input.length());
if (Username.equals(userTextField) || (Password.equals(pwBox))) {
found = true; // added this to set found
System.out.println("Success!");
}
if (!found) {
JOptionPane.showMessageDialog(null,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ProjectLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
});