Java 而以hasNext方法作为条件的循环继续抛出ArrayOutOfBounds异常?(爪哇)
我正在使用GUI进行用户名/密码登录。我使用了一个while循环,文件名的扩展名是hasNext(),作为将文件中的信息存储到数组中的条件,这样我就可以将用户的输入与存储在文件中的信息进行比较。然后我使用if语句进行比较。如果存在匹配项,则程序会将用户重定向到另一个类或方法,否则不会执行任何操作 问题是,当ArrayOutOfBounds异常试图从文件中读取第一行时,我就一直抛出它 我加入了一个System.out.println(userLogin[I]),以查看它是否一直循环,但它不能,因为该行从未输出。我的猜测是,一旦进入while循环,它首先会停止。我该如何解决这个问题?非常感谢你的帮助。如果你需要我澄清这个问题,请在你的评论中这样说,我会重新编辑我的帖子,使其尽可能清晰。这是我的代码,如下所示:Java 而以hasNext方法作为条件的循环继续抛出ArrayOutOfBounds异常?(爪哇),java,arrays,file,bounds,out,Java,Arrays,File,Bounds,Out,我正在使用GUI进行用户名/密码登录。我使用了一个while循环,文件名的扩展名是hasNext(),作为将文件中的信息存储到数组中的条件,这样我就可以将用户的输入与存储在文件中的信息进行比较。然后我使用if语句进行比较。如果存在匹配项,则程序会将用户重定向到另一个类或方法,否则不会执行任何操作 问题是,当ArrayOutOfBounds异常试图从文件中读取第一行时,我就一直抛出它 我加入了一个System.out.println(userLogin[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); //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++;
}
}
文件中的信息以这种方式写入(左边是用户名,右边是密码):
谢谢大家!
这是全部代码,很抱歉之前没有包含。希望这能帮助你们更好地理解我的问题。再次感谢您抽出时间回答
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.userLogin
与userLogin
不同。如果我不在,我道歉。无意冒犯。据我所知,您将userLogin设置为零长度数组,并且永远不要增加它:
private String userLogin[] = {};
因此,访问userLogin的任何元素都会引发异常。它实际上与inputFile.next()
调用没有任何关系。您可以执行一个隔离的userLogin[0]=null
,它应该会爆炸
我建议您使用ArrayList或其他可以增量构建的动态大小结构。您将这3个设置为数组,但您从未将它们用作数组,也从未在verifyLogin之外使用它们
private String userLogin[] = {};
private String passLogin[] = {};
private char passwordCheckLogin[] = {};
这将需要额外的代码,但应该让您朝着正确的方向前进
我已经为这3个变量创建了局部变量,并调整了代码以使用它们
public void verifyLogin(String username, char[] password) throws IOException {
File myFile = new File("Accounts.txt");
Scanner inputFile = new Scanner(myFile);
String userLogin = null;
String passLogin = null;
char[] passwordCheckLogin = null;
while (inputFile.hasNext()) {
userLogin = inputFile.next();
if (inputFile.hasNext()){
passLogin = inputFile.next();
passwordCheckLogin = passLogin.toCharArray();
}
else {
//Need to alert that we have an improperly formatted Accounts.txt
break;
}
if((userLogin == username) && (passwordCheckLogin == password)){
JOptionPane.showMessageDialog(null, "Access Granted");
inputFile.close();
break;
}
}
}
首先,你不需要扫描仪。BufferedReader更简单。您可以使用“拆分”将单词分隔成一行。另外,请注意流闭合的位置(在末尾)。此外,我还使用equals而不是==重新调整了条件。最后,输入String.valueOf以允许将字符数组与字符串进行比较
File myFile = new File("Accounts.txt");
BufferedReader inputFile = new BufferedReader(
new InputStreamReader(new FileInputStream(myFile)));
String line;
while ((line = inputFile.readLine())!=null) {
int i = 0;
this.userLogin[i] = line.split(" ")[0];
System.out.println(userLogin[i]);
this.passLogin[i] = line.split(" ")[1];
this.passwordCheckLogin = this.passLogin[i].toCharArray();
if (this.userLogin[i].equals(username)
&& String.valueOf(this.passwordCheckLogin).equals(password)){
JOptionPane.showMessageDialog(null, "Access Granted");
break;
}
i++;
}
inputFile.close();
我怀疑例外情况是否在您声明的那一行,因为您在它上面的那一行访问该索引。尽管有明显的错误(i
始终为0,例如)。您能给我们一个自包含的代码片段来实际演示这个问题吗?你给我们的是一个粗略的副本,它不能忠实地复制你的代码。数组userLogin[]
是否正确声明和初始化?你在哪里填充userLogin[]数组?它有多长?因为你总是设置i=0
和this.userLogin[i]=inputFile.next()代码>正在工作,那么就没有办法System.out.println(userLogin[i])代码>将给出indexootfboundexception
。请重新查看您的代码。问题出在别的地方。也i++接近结尾的code>是无用的,因为开始时i
被设置为0
。or next()的返回类型是String。userLogin[i]是如何工作的?userLogin[]是字符串数组吗?
File myFile = new File("Accounts.txt");
BufferedReader inputFile = new BufferedReader(
new InputStreamReader(new FileInputStream(myFile)));
String line;
while ((line = inputFile.readLine())!=null) {
int i = 0;
this.userLogin[i] = line.split(" ")[0];
System.out.println(userLogin[i]);
this.passLogin[i] = line.split(" ")[1];
this.passwordCheckLogin = this.passLogin[i].toCharArray();
if (this.userLogin[i].equals(username)
&& String.valueOf(this.passwordCheckLogin).equals(password)){
JOptionPane.showMessageDialog(null, "Access Granted");
break;
}
i++;
}
inputFile.close();