我正在使用 GUI 进行用户名/密码登录。我正在使用带有文件名的 hasNext() 扩展名的 while 循环作为将文件中的信息存储到数组中的条件,以便我可以将用户的输入与文件中存储的信息进行比较。然后我使用 if 语句进行比较。如果匹配,则程序将用户重定向到另一个类或方法,否则它不做任何事情。
问题是我一直在抛出一个 ArrayOutOfBounds 异常,因为它试图从文件中读取第一行。
我扔了一个 System.out.println(userLogin[i]) 来查看它是否一直循环,但它一定不能,因为该行从不输出。我的猜测是,一旦进入 while 循环,它就会首先停止。我该如何解决?非常感谢您的帮助。如果您需要我澄清这个问题,请在您的评论中说出来,我将重新编辑我的帖子以尽可能清晰。这是我的代码如下:
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
String usernameInput = "";
char passwordInput[] = {};
if(e.getSource() == okButton){
usernameInput = usernameTF.getText();
passwordInput = passwordTF.getPassword();
try{
verifyLogin(usernameInput, passwordInput); //sends input to be verified
}catch (IOException ed){
JOptionPane.showMessageDialog(null, "There was a problem " +
"retrieving the data.");
}
}else if (e.getSource() == cancelButton){
setVisible(false);
dispose();
}
}
}
那是应该将用户输入的用户名和密码发送到检查文件中存储的内容是否匹配的方法的类和方法。
这是验证输入是否匹配的方法:
public void verifyLogin(String username, char[] password) throws IOException {
File myFile = new File("Accounts.txt");
Scanner inputFile = new Scanner(myFile);
while (inputFile.hasNext()) {
int i = 0;
this.userLogin[i] = inputFile.next();
System.out.println(userLogin[i]); //says this is where the OutOfBounds occurs
this.passLogin[i] = inputFile.nextLine();
this.passwordCheckLogin = this.passLogin[i].toCharArray();
if((this.userLogin[i] == username) && (this.passwordCheckLogin == password)){
JOptionPane.showMessageDialog(null, "Access Granted");
inputFile.close();
break;
}
i++;
}
}
文件中的信息是这样写的(左边是用户名,右边是密码):
John001 bananas
Mike001 123chocolate
谢谢!
这是整个代码,抱歉之前没有包含它。希望这可以帮助你们更好地理解我的问题。再次感谢您抽出时间来回答。
import java.util.Scanner;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class DeskLogin extends JFrame {
private final int WINDOW_WIDTH = 200;
private final int WINDOW_HEIGHT = 300;
private JLabel usernameLabel;
private JLabel passwordLabel;
private JButton okButton;
private JButton cancelButton;
private JTextField usernameTF;
private JPasswordField passwordTF;
private JPanel loginPanel;
private String userLogin[] = {};
private String passLogin[] = {};
private char passwordCheckLogin[] = {};
public DeskLogin(){
super("Login");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
buildPanel();
add(loginPanel);
}
private void buildPanel() {
usernameLabel = new JLabel("Username: ");
passwordLabel = new JLabel("Password: ");
usernameTF = new JTextField(10);
passwordTF = new JPasswordField(10);
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
loginPanel = new JPanel();
loginPanel.add(usernameLabel);
loginPanel.add(usernameTF);
loginPanel.add(passwordLabel);
loginPanel.add(passwordTF);
loginPanel.add(okButton);
loginPanel.add(cancelButton);
okButton.addActionListener(new ButtonListener());
cancelButton.addActionListener(new ButtonListener());
}
public void verifyLogin(String username, char[] password) throws IOException {
File myFile = new File("Accounts.txt");
Scanner inputFile = new Scanner(myFile);
inputFile.useDelimiter(" ");
while (inputFile.hasNext()) {
int i = 0;
this.userLogin[i] = inputFile.next();
System.out.println(userLogin[i]);
this.passLogin[i] = inputFile.nextLine();
this.passwordCheckLogin = this.passLogin[i].toCharArray();
if((this.userLogin[i] == username) && (this.passwordCheckLogin == password)){
JOptionPane.showMessageDialog(null, "Access Granted");
inputFile.close();
break;
}
i++;
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
String usernameInput = "";
char passwordInput[] = {};
if(e.getSource() == okButton){
usernameInput = usernameTF.getText();
passwordInput = passwordTF.getPassword();
try{
verifyLogin(usernameInput, passwordInput);
}catch (IOException ed){
JOptionPane.showMessageDialog(null, "There was a problem " +
"retrieving the data.");
}
}else if (e.getSource() == cancelButton){
setVisible(false);
dispose();
}
}
}
}