Home » Java » Username and Password verification in Java ,CSV file-Exceptionshub

Username and Password verification in Java ,CSV file-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

My code is a simple Username and Password login application, the user creates a new username and creates a new password and these data are stored in a CSV file.

I want to write a code that verifies the username and password stored in the CSV file if the user tries to log in.

I don’t know where to start, should I create a reader?
and if so, how can I make the reader stop when it sees the same String value of the username?

this is my code that should add the usernames and passwords in the CSV file.

public void setCreateButton() throws Exception {

    String newUsername = usernameTextField.getText();
    String newUserPassword = passwordField.getText();
    String confirmedPassword = confirmPasswordField.getText();

    if (newUserPassword.equals(confirmedPassword)) {
        Path filePath = Paths.get("D:\Users\login.csv");

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(newUsername);
        stringBuilder.append(',');
        stringBuilder.append(newUserPassword);
        stringBuilder.append(System.lineSeparator());

        Files.write(filePath, stringBuilder.toString().getBytes(), StandardOpenOption.APPEND);

        Stage stage = (Stage) createButton.getScene().getWindow();
        stage.close();
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Successfully Created");
        alert.setHeaderText(null);
        alert.setContentText('\n' + "User Created Successfully");
        alert.show();
    } else {
        System.out.println("Password is not matched");
    }
}

It’s for the “Create User” button, I want to make something for the “Login” button after been pressed.

How to&Answers: