【发布时间】:2019-07-15 07:20:31
【问题描述】:
我正在登录从 2 个文本文件读取的 gui。用户名行与密码中的行匹配。如果用户名在第 3 行,那么密码将在 password.txt 的第 3 行 如果用户名和密码在 username.txt 和 password.txt 中匹配,系统将让您登录。我可以从用户名文本,但在读取 password.txt 时我一直为空。
我尝试使用扫描仪,但我不需要阅读每一行。我只需要阅读特定的行。
private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
try {
File f1=new File("username.txt");
File f2=new File ("password.txt");
String[] words=null;
FileReader fr1 = new FileReader(f1);
FileReader fr2 = new FileReader(f2);
BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
String s;
String user = username.getText();
String pass=String.valueOf(password.getPassword());
String usertxt = " ";
String passtxt=" ";
int count =0;
while((s=br1.readLine())!=null) //Reading Content from the file
{
words=s.split(" "); //Split the word using space
for (String word : words)
{
if (word.equals(user)) //Search for the given word
{
System.out.print(count);
for (int i = 0; i < count; i++)
{
br2.readLine();
passtxt = br2.readLine();
}
System.out.print(passtxt);
if(pass.equals(passtxt))
JOptionPane.showMessageDialog(null,"User has logged in");
}
}
count++;
}
}
catch (IOException e)
{
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
}
}
我希望字符串传递等于 passtxt。
【问题讨论】:
-
你的函数的哪一部分是“我得到的都是空的”发生?你的意思是你得到一个 NullPointerException 吗?如果是这样,它发生在哪一行?
标签: java string user-interface readfile