在为注册和登录系统创建注册类时,它会成功将用户添加到文本文件中。我要问的是,如果用户的登录ID已存在于文本文件中,我可以如何检查程序。
文本文件的内容:
User{nickname= Barry, loginID= baz15, password= dan}
输入相同的注册详细信息时,会再次添加。如何才能将其重新添加到文件中,而是显示JOptionPane
?
我有一个引用User类的数组列表,以检查用户是否存在于文本文件中。到目前为止,这是我的代码:
public class RegistrationJFrame extends javax.swing.JFrame {
ArrayList<User> users = new ArrayList<>();
public RegistrationJFrame() {
initComponents();
lblErrorMessage.setVisible(false);
}
private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {
String nickname = edtNickname.getText();
String loginID = edtLoginID.getText();
String password = String.valueOf(edtPassword.getPassword());
String confirmPassword = String.valueOf(edtReenterPassword.getPassword());
if (!password.equals(confirmPassword)) {
lblErrorMessage.setText("Passwords do not match");
}
if (nickname.equals("") || loginID.equals("") || password.equals("")) {
lblErrorMessage.setText("You must fill in the text fields");
}
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getLoginID().equals(loginID)) {
JOptionPane.showMessageDialog(null, "User exists in file");
} else {
JOptionPane.showMessageDialog(null, "User does not exist");
}
}
try {
User user = new User(nickname, loginID, password);
users.add(user);
File filename = new File("userinfo.txt");
if (!filename.exists()) {
filename.createNewFile();
}
FileWriter fw = new FileWriter(filename, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(user.toString());
bw.newLine();
bw.close();
} catch (Exception e) {
System.out.println("");
}
}
}
答案 0 :(得分:1)
如果我理解你是对的,我会用一个单独的方法来做:
public class RegistrationJFrame extends javax.swing.JFrame {
ArrayList<User> users = new ArrayList<>();
public RegistrationJFrame() {
initComponents();
lblErrorMessage.setVisible(false);
}
private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {
String nickname = edtNickname.getText();
String loginID = edtLoginID.getText();
String password = String.valueOf(edtPassword.getPassword());
String confirmPassword = String.valueOf(edtReenterPassword.getPassword());
if (!password.equals(confirmPassword)) {
lblErrorMessage.setText("Passwords do not match");
}
if (nickname.equals("") || loginID.equals("") || password.equals("")) {
lblErrorMessage.setText("You must fill in the text fields");
}
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getLoginID().equals(loginID)) {
JOptionPane.showMessageDialog(null, "User exists in file");
} else {
JOptionPane.showMessageDialog(null, "User does not exist");
User user = new User(nickname, loginID, password);
users.add(user);
saveUser(user);
}
}
}
private void saveUser(User user) {
try {
File filename = new File("userinfo.txt");
if (!filename.exists()) {
filename.createNewFile();
}
FileWriter fw = new FileWriter(filename, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(user.toString());
bw.newLine();
bw.close();
} catch (Exception e) {
System.out.println("");
}
}
}
对于您的文件,您可以使用json文件更好。如果要向用户添加更多信息,可能会更容易。
答案 1 :(得分:1)
一个简单的解决方法是将用户重写为文件,而不是将新用户附加到您的文件中。这意味着您只需循环遍历列表,并在每次创建新用户时重写所有用户的文件。您可以像这样创建一个saveToFile()方法并将其命名为
private void saveToFile()
{
try{
File filename = new File("userinfo.txt");
if (!filename.exists()) {
filename.createNewFile();
}
FileWriter fw = new FileWriter(filename, false); //false so you don't append and overwrite
BufferedWriter bw = new BufferedWriter(fw);
//now loop your arraylist of users to resave them to your file
for(User currUser : users)
{
bw.write(currUser.toString());
bw.newLine();
}
bw.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
接下来,您必须确定何时添加用户并保存即可。尝试在btnRegisterActionPerformed方法中使用布尔值,如下所示
private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {
String nickname = edtNickname.getText();
String loginID = edtLoginID.getText();
String password = String.valueOf(edtPassword.getPassword());
String confirmPassword = String.valueOf(edtReenterPassword.getPassword());
if (!password.equals(confirmPassword)) {
lblErrorMessage.setText("Passwords do not match");
}
if (nickname.equals("") || loginID.equals("") || password.equals("")) {
lblErrorMessage.setText("You must fill in the text fields");
}
boolean addUser = true;
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getLoginID().equals(loginID)) {
JOptionPane.showMessageDialog(null, "User exists in file");
addUser = false;
break;//no need to keep checking so break out of the for loop
} else {
JOptionPane.showMessageDialog(null, "User does not exist");
}
}
if(addUser)
{
User user = new User(nickname, loginID, password);
users.add(user);
//now save the contents of the list to the file
saveToFile();
}
}
您目前始终将用户添加到您的文件中,这是不正确的。我们使用布尔值来确定用户是否不存在,并且只有在找不到用户时才会添加用户并保存到文件中。