2017-05-24 171 views
-1

我有一個名爲「AddUser」的程序,允許用戶鍵入他們的用戶名和密碼,這會將此信息添加到user.txt文件。我也有一個名爲「登錄」程序,它的信息的用戶輸入用戶名和密碼,並驗證對user.txt文件的輸入。Javafx驗證用戶輸入用戶名和密碼

但是,我想不出如何驗證的登錄程序的輸入。我在這裏發現了其他幾個帖子,但不是從文本文件驗證。任何幫助或指導將非常感激。

程序添加用戶

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

程序登錄

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); 
    } 
} 
+0

通讀文件中的每一行,成對。如果第一行是相同的從用戶的文本字段中的文本,並且所述第二線是一樣的文本形成密碼文本字段,然後設定登錄爲true。如果您沒有匹配到文件末尾,請執行您想要執行的操作以向用戶顯示登錄名不正確。 –

回答

1

考慮這個(在評論說明):

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

如果*讀取密碼*線,你不需要增加'再次i'?事實上,爲什麼你需要循環使用計數器? –

+0

@ScaryWombat乾杯的人,我錯過了。 – Yahya

+0

'而(s.hasNextLine()){...}'' –