java - 以 hasNext 方法为条件的 while 循环不断抛出 ArrayOutOfBounds 异常? ( java )

标签 java arrays file bounds out

我正在使用 GUI 进行用户名/密码登录。我使用 while 循环,将文件名扩展为 hasNext() 作为将文件中的信息存储到数组中的条件,以便我可以将用户的输入与文件中存储的信息进行比较。然后我使用 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();
    }
}

}








}

最佳答案

您尝试将行更改为 System.out.println(this.userLogin[i]); 怎么样? ?我的猜测是,您有两个名为 userLogin 的变量,其中一个是该函数的本地变量(另一个是实例变量)。 this.userLoginuserLogin 不同。如果我休息了,抱歉。无意冒犯。

关于java - 以 hasNext 方法为条件的 while 循环不断抛出 ArrayOutOfBounds 异常? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13737121/

相关文章:

PHP:在循环中合并数组

javascript - 从数组中删除多余的空格

java - 将 JPA 对象转换为 XML/JAXB

linux - 如何在CentOS linux上安装特定版本的jdk

java - 将两个 JComponent 添加到 JFrame BorderLayout 的北部

php - 合并来自不同文件的 2 个数组

c - 如何在 C 中将实时音频数据(更新缓冲区)呈现为音频文件

java - 以 RESTful 方式上传文件?

c# - 如果文件存在,覆盖它?

java - 来自 Java 的 mxGraph (www.jgraph.com) 的 XML 可视化