so I am trying to write a login form using javafx. The code is supposed to read the username and their relative password from a text file and then when pressed on sign button an alert should pops up and it tells me successfully sign in. Incase password or username is wrong another alert should be displayed telling the user that there is a mistake. Problem is that the code is reading the credentials from the file still it is executing both conditions regardless whether the data entered is true or false. This is my code:
sign.setOnAction(e -> {
File file = new File("users.txt");
Scanner scan = null;
try {
scan = new Scanner(file);
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
while (scan != null && scan.hasNext()) {
userRead = scan.next();
passRead = scan.next();
User = usernametf.getText();
Pass = passwordtf.getText();
if (User.equals(userRead) && Pass.equals(passRead)) {
Alert A = new Alert(Alert.AlertType.INFORMATION);
A.setTitle("Sign In");
A.setContentText("Sign In Successfully");
A.showAndWait();
} else {
Alert B = new Alert(Alert.AlertType.INFORMATION);
B.setTitle("Blank Fields");
B.setContentText("Fields are required");
B.showAndWait();
}
}
});
Aucun commentaire:
Enregistrer un commentaire