我有一个名为samplename的txt文件,在该文件中有3行值:
samplename
samplepassword
%%%%%%%%%%%%%%%%%
该程序是关于登录注册系统的。 当我输入
samplename
在第一个JTextField和
中 samplepassword +
在第二个JTextField中按下登录按钮,它应该检查我是否有这样的帐户,如果它有正确的用户名和密码。 程序读取内容,只是它不能与JTextFields进行比较。我做错了什么?!?:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class SomeClass
{
public JButton loginButton;
public JTextField login_username;
public JTextField login_password;
String check = null;
String check2 = null;
public static void main(String[] args)
{
SomeClass someClass = new SomeClass();
someClass.doSomething();
}
public void doSomething()
{
loginButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Scanner scanner = new Scanner(System.in);
BufferedReader rd = null;
String line;
boolean gothrew = false;
boolean ifDOES = false;
String Username;
String Password;
Password = login_username.getText().trim();
Username = login_password.getText().trim();
try
{
rd = new BufferedReader(new FileReader(
"C:\\users\\Dominik Gyarmati\\AppData\\Roaming\\KeepSafe\\users\\"
+ login_username.getText() + ".txt"));
}
catch (FileNotFoundException ex)
{
JOptionPane.showMessageDialog(null, "Your Account doesn't exist");
}
try
{
while ((line = rd.readLine()) != null)
{
if (check == null)
{
check = line;
check.trim();
gothrew = true;
System.out.println(check);
}
else if (gothrew == true)
{
check2 = line;
check2.trim();
System.out.println(check2);
gothrew = false;
ifDOES = true;
}
else if (check.equals(Username) && check2.equals(Password))
{
JOptionPane.showMessageDialog(null,
"You've logged into your account");
check = null;
check2 = null;
ifDOES = false;
}
else
{
JOptionPane.showMessageDialog(null,
"Username or Password is wrong");
check = null;
check2 = null;
ifDOES = false;
}
}
}
catch (IOException ioexex)
{}
}
});
}
}
编辑: 我只需要改变
else if (check.equals(Username) && check2.equals(Password))
到
else if (check.equals(login_username.getText().trim()) &&
check2.equals(login_password.getText().trim())) {
答案 0 :(得分:0)
首先,你的代码闻起来很糟糕:
其次,尝试使用调试工具:完美地调试问题的答案。
最后,检查以下几行:
Password = login_username.getText().trim();
Username = login_password.getText().trim();
将Password
替换为Username
,反之亦然。