java - 不确定使用 BufferedReader 根据 boolean 值检查 .txt 文件中的信息

标签 java javafx bufferedreader

我在正确检查从 .txt 文件读取的代码时遇到问题。我可以更改代码以使其打印字符串。但我无法让它根据当前输入的信息检查字符串。我收到了我编码的错误消息,该消息弹出了 3 次。我的 user.txt 文件采用用户名:密码信息,如下所示 CPlank000:Tech8120 这是代码,

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.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.*;
import javax.swing.JOptionPane;


public class Login extends Application {
private TextField tfUsername = new TextField();
private TextField tfPassword = new PasswordField();
private Button btLogin = 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(btLogin, 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(btLogin, HPos.CENTER);
GridPane.setHalignment(btClear, HPos.RIGHT);

// Process events
btLogin.setOnAction(e -> checkLogin());

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
 }

public void checkLogin() {
 try (BufferedReader br = new BufferedReader(new FileReader("user.txt")))
    {

        String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
            if ("tfUsername" == sCurrentLine)  {
                if ("tfPassword" == sCurrentLine)
                JOptionPane.showMessageDialog(null, "Welcome!!");}
                else
                    JOptionPane.showMessageDialog(null, "Invalid Username or Password");    
            }


    } catch (IOException e) {
        System.out.println("Unable to find");
        System.out.println(e.getMessage());

    } 

}





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

最佳答案

问题:

           if ("tfUsername" == sCurrentLine)  {
            if ("tfPassword" == sCurrentLine)

您正在比较字符串的内存位置,而不是它的真值,因此它始终返回 false,请改用 equals 方法。

示例:

           if ("tfUsername".equals(sCurrentLine))  {
            if ("tfPassword".equals(sCurrentLine))

解决方案:

您可以拆分字符串以分别获取用户名和密码,因为您所做的是比较整行字符串

while ((sCurrentLine = br.readLine()) != null) {
        String [] s = sCurrentLine.split(":"); //split the string

        if ("tfUsername".equals(s[0])) {
            if ("tfPassword".equals(s[1]))
                JOptionPane.showMessageDialog(null, "Welcome!!");
        } else
            JOptionPane.showMessageDialog(null,"Invalid Username or Password");
    }

关于java - 不确定使用 BufferedReader 根据 boolean 值检查 .txt 文件中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24566767/

相关文章:

JavaFX 嵌入自定义字体不起作用

java - 2 个 Java 类中的公共(public)私有(private)变量连接问题

java - 如何修改 TreeTableView 的点击行为

java - 如何在 Javafx 上用鼠标单击按钮更改 ImageView 的图像(src)?

java - 如何检测(读取中的关闭输入)

java - 如何解决系统找不到指定文件的错误?

java - 递归方法没有输出

java - Java 中的词法、句法或语义错误差异

java - Spring + Spring Security + Hibernate org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary

java - 在构造函数中使用对可重写方法的引用是否安全?