0

I have a program called "AddUser" that allows the user to type in their username and password, which will add this info to user.txt file. I also have a program called "Login" that takes the information the user inputs, username and password, and verifies the input against the user.txt file.

However, I cannot figure out how to validate the input for the Login program. I have found several other posts here, but not from validating from a text file. Any help or guidance would be GREATLY appreciated.

Program Add User

    import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.io.*;

public class AddUser extends Application {
    private TextField tfUsername = new TextField();
    private TextField tfPassword = new TextField();
    private Button btAddUser = new Button("Add User");
    private Button btClear = new Button("Clear");

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Create UI
        GridPane gridPane = new GridPane();
        gridPane.setHgap(5);
        gridPane.setVgap(5);
        gridPane.add(new Label("Username:"), 0, 0);
        gridPane.add(tfUsername, 1, 0);
        gridPane.add(new Label("Password:"), 0, 1);
        gridPane.add(tfPassword, 1, 1);
        gridPane.add(btAddUser, 1, 3);
        gridPane.add(btClear, 1, 3);

        // Set properties for UI
        gridPane.setAlignment(Pos.CENTER);
        tfUsername.setAlignment(Pos.BOTTOM_RIGHT);
        tfPassword.setAlignment(Pos.BOTTOM_RIGHT);

        GridPane.setHalignment(btAddUser, HPos.LEFT);
        GridPane.setHalignment(btClear, HPos.RIGHT);

        // Process events
        btAddUser.setOnAction(e -> writeNewUser());

        btClear.setOnAction(e -> {
            tfUsername.clear();
            tfPassword.clear();
        });   

        // Create a scene and place it in the stage
        Scene scene = new Scene(gridPane, 300, 150);
        primaryStage.setTitle("Add User"); // Set title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }

    public void writeNewUser() {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("users.txt", true))) {
            bw.write(tfUsername.getText());
            bw.newLine();
            bw.write(tfPassword.getText());
            bw.newLine();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * The main method is only needed for the IDE with limited
     * JavaFX support. Not needed for running from the command line.
     */
    public static void main(String[] args) {
        launch(args);
    }
}

Program Login

    import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.io.*;

public class Login extends Application {
    private TextField tfUsername = new TextField();
    private TextField tfPassword = new TextField();
    private Button btAddUser = new Button("Login");
    private Button btClear = new Button("Clear");

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Create UI
        GridPane gridPane = new GridPane();
        gridPane.setHgap(5);
        gridPane.setVgap(5);
        gridPane.add(new Label("Username:"), 0, 0);
        gridPane.add(tfUsername, 1, 0);
        gridPane.add(new Label("Password:"), 0, 1);
        gridPane.add(tfPassword, 1, 1);
        gridPane.add(btAddUser, 1, 3);
        gridPane.add(btClear, 1, 3);

        // Set properties for UI
        gridPane.setAlignment(Pos.CENTER);
        tfUsername.setAlignment(Pos.BOTTOM_RIGHT);
        tfPassword.setAlignment(Pos.BOTTOM_RIGHT);

        GridPane.setHalignment(btAddUser, HPos.LEFT);
        GridPane.setHalignment(btClear, HPos.RIGHT);

        // Process events
        btClear.setOnAction(e -> {
            tfUsername.clear();
            tfPassword.clear();
        });   

        // Create a scene and place it in the stage
        Scene scene = new Scene(gridPane, 300, 150);
        primaryStage.setTitle("Login"); // Set title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }

    /**
     * The main method is only needed for the IDE with limited
     * JavaFX support. Not needed for running from the command line.
     */
    public static void main(String[] args) {
        launch(args);
    }
}   
MilesDeo
  • 21
  • 1
  • 3
  • Read through each line in the file, in pairs. If the first line is the same as the text from the user text field, and the second line is the same as the text form the password text field, then set login to true. If you get to the end of the file with no match, then do whatever you want to do to show the user the login is incorrect. – James_D May 24 '17 at 00:46

1 Answers1

1

Consider this Example (Explanation in Comments):

// create boolean variable for final decision
boolean grantAccess = false;

// get the user name and password when user press on login button
// you already know how to use action listener 
// (i.e wrap the following code with action listener block of login button)
String userName = tfUsername.getText();
String password = tfPassword.getText();

File f = new File("users.txt");
try {
     Scanner read = new Scanner(f); 
     int noOfLines=0; // count how many lines in the file
     while(read.hasNextLine()){
           noOfLines++;
     }

    //loop through every line in the file and check against the user name & password (as I noticed you saved inputs in pairs of lines)
    for(int i=0; i<noOfLines; i++){
       if(read.nextLine().equals(userName)){ // if the same user name
          i++;
          if(read.nextLine().equals(password)){ // check password
             grantAccess=true; // if also same, change boolean to true
             break; // and break the for-loop
          }
       }
    }
     if(grantAccess){
        // let the user continue 
        // and do other stuff, for example: move to next window ..etc
     }
     else{
         // return Alert message to notify the deny
     }

} catch (FileNotFoundException e) {

        e.printStackTrace();
}
Yahya
  • 9,370
  • 4
  • 26
  • 43