Java 不确定是否使用BufferedReader根据布尔值检查.txt文件中的信息
我在正确检查从.txt文件读取的代码时遇到问题。我可以更改代码,让它打印字符串。但我无法让它根据当前输入的信息检查字符串。我收到我编码的错误信息,它会弹出3次。我的user.txt文件中包含用户名:密码信息,如CPlank000:Tech8120这是代码Java 不确定是否使用BufferedReader根据布尔值检查.txt文件中的信息,java,javafx,bufferedreader,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.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)
if ("tfUsername".equals(sCurrentLine)) {
if ("tfPassword".equals(sCurrentLine))
您正在比较字符串的内存位置,而不是它的真值,因此它总是返回false,请改用equals
方法
示例:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
if ("tfUsername".equals(sCurrentLine)) {
if ("tfPassword".equals(sCurrentLine))
解决方案:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
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");
}
问题:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
if ("tfUsername".equals(sCurrentLine)) {
if ("tfPassword".equals(sCurrentLine))
您正在比较字符串的内存位置,而不是它的真值,因此它总是返回false,请改用equals
方法
示例:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
if ("tfUsername".equals(sCurrentLine)) {
if ("tfPassword".equals(sCurrentLine))
解决方案:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
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");
}
问题:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
if ("tfUsername".equals(sCurrentLine)) {
if ("tfPassword".equals(sCurrentLine))
您正在比较字符串的内存位置,而不是它的真值,因此它总是返回false,请改用equals
方法
示例:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
if ("tfUsername".equals(sCurrentLine)) {
if ("tfPassword".equals(sCurrentLine))
解决方案:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
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");
}
问题:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
if ("tfUsername".equals(sCurrentLine)) {
if ("tfPassword".equals(sCurrentLine))
您正在比较字符串的内存位置,而不是它的真值,因此它总是返回false,请改用equals
方法
示例:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
if ("tfUsername".equals(sCurrentLine)) {
if ("tfPassword".equals(sCurrentLine))
解决方案:
if ("tfUsername" == sCurrentLine) {
if ("tfPassword" == sCurrentLine)
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");
}
必须使用equals()来比较字符串值,'=='检查是检查引用相等而不是值。必须使用equals()来比较字符串值,'='检查是检查引用相等而不是值。必须使用equals()来比较字符串值,“==”检查是检查引用相等而不是值。必须使用equals()来比较字符串值,“==”检查是检查引用相等而不是值。在checkLogin()方法中,使用==运算符来比较字符串对象。==运算符comapares引用非基元类型。它只测试两个变量是否包含对同一对象的引用
java中的每个对象都至少有一个前置->对象。对象定义了几个方法,其中有equals()和hashCode(),用于比较相同类型的两个对象。字符串重写这些方法。因此,与其使用
//returns true only if those are same objects in memory
"tsUsername" == sCurrentLine
你应该用
//returns true if content of compared String contain exactly the same characters
sCurrentLine.equals("tsusername");
但即使在那之后,你的代码也可能无法工作。比较整行。在checkLogin()方法中,使用==运算符比较字符串对象。==运算符comapares引用非基元类型。它只测试两个变量是否包含对同一对象的引用
java中的每个对象都至少有一个前置->对象。对象定义了几个方法,其中有equals()和hashCode(),用于比较相同类型的两个对象。字符串重写这些方法。因此,与其使用
//returns true only if those are same objects in memory
"tsUsername" == sCurrentLine
你应该用
//returns true if content of compared String contain exactly the same characters
sCurrentLine.equals("tsusername");
但即使在那之后,你的代码也可能无法工作。比较整行。在checkLogin()方法中,使用==运算符比较字符串对象。==运算符comapares引用非基元类型。它只测试两个变量是否包含对同一对象的引用
java中的每个对象都至少有一个前置->对象。对象定义了几个方法,其中有equals()和hashCode(),用于比较相同类型的两个对象。字符串重写这些方法。因此,与其使用
//returns true only if those are same objects in memory
"tsUsername" == sCurrentLine
你应该用
//returns true if content of compared String contain exactly the same characters
sCurrentLine.equals("tsusername");
但即使在那之后,你的代码也可能无法工作。比较整行。在checkLogin()方法中,使用==运算符比较字符串对象。==运算符comapares引用非基元类型。它只测试两个变量是否包含对同一对象的引用
java中的每个对象都至少有一个前置->对象。对象定义了几个方法,其中有equals()和hashCode(),用于比较相同类型的两个对象。字符串重写这些方法。因此,与其使用
//returns true only if those are same objects in memory
"tsUsername" == sCurrentLine
你应该用
//returns true if content of compared String contain exactly the same characters
sCurrentLine.equals("tsusername");
但即使在那之后,你的代码也可能无法工作。您可以将其与整行进行比较。即使我在.txt文件中输入了我知道的信息,仍然会抛出我的“else”错误4次。@planker1010文本文件中的内容发布。CPlank000:Tech8120 BSmith000:Tech8237 LDrogula000:Tech8131 CMyers000:Tech8083@planker1010见上文。s[0]是用户名,s[1]是密码,但这会检查整个字符串吗?如果有50个名字,它会全部检查吗?即使我在.txt文件中输入我知道的信息,它仍然会抛出我的“else”错误4次。@planker1010文本文件中的内容发布它。CPlank000:Tech8120 BSmith000:Tech8237 LDrogula000:Tech8131 CMyers000:Tech8083@planker1010见上文。s[0]是用户名,s[1]是密码,但这会检查整个字符串吗?如果有50个名字,它会全部检查吗?即使我在.txt文件中输入我知道的信息,它仍然会抛出我的“else”错误4次。@planker1010文本文件中的内容发布它。CPlank000:Tech8120 BSmith000:Tech8237 LDrogula000:Tech8131 CMyers000:Tech8083@planker1010见上文。s[0]是用户名,s[1]是密码,但这会检查整个字符串吗?如果有50个名字,它会全部检查吗?还是