如何使用GUI正确比较文本文件中的行与用户输入

时间:2015-06-28 10:55:38

标签: java string user-interface bufferedreader filewriter

我一直在互联网上漫游收集信息并使用它们但我似乎无法成功地将文本文件中的一行文本与使用gui的用户输入进行比较

文件名:MainLogin.Java

String user, pass;

user = txtUser.getText();
pass = txtPass.getText();

try (BufferedReader br = new BufferedReader(new FileReader("profiles.txt"))) {
    String sCurrentLine;

    while ((sCurrentLine = br.readLine()) != null) {
        System.out.println(sCurrentLine);

        if (sCurrentLine.equals(user) && sCurrentLine.equals(pass)) {
            JOptionPane.showMessageDialog(null, "Login Successful!");
            MainWindow mw = new MainWindow();
            dispose();
        }
        else {
            JOptionPane.showMessageDialog(null, "Wrong Username and/or Password!");
        }
    }
}
catch (IOException io) {
    io.printStackTrace();
}

以下是我使用"注册"在文本文件中写的方式。方法

文件名:Registration.java

String name, user, pass, address;
int contactNo;
float deposit;

name = txtName.getText();
user = txtUser.getText();
pass = txtPass.getText();
address = txtPass.getText();
contactNo = Integer.parseInt(txtContact.getText());
deposit = Float.parseFloat(txtDeposit.getText());

//u = new User(name , user, pass, address, contactNo, deposit);

try {
    writer = new BufferedWriter(new FileWriter("profiles.txt", true));
    writer.write(name + "\r\n");
    writer.write(user + "\r\n");
    writer.write(pass + "\r\n");
    writer.write(address + "\r\n");
    writer.write(contactNo + "\r\n");
    writer.write(Float.toString(deposit) + "\r\n");
    //writer.write("------------------------------------------------------------");
}
catch (IOException ex) {
    // report
}
finally {
    try {
        writer.close();
    }
    catch (Exception ex) {/*ignore*/}
}

JOptionPane.showMessageDialog(null, "Registered!");

MainLogin ml = new MainLogin();
dispose();

0 个答案:

没有答案