我正在使用GUI進行用戶名/密碼登錄。我正在使用帶文件名的hasNext()擴展名的while循環作爲條件,將文件中的信息存儲到數組中,以便我可以將用戶的輸入與存儲在文件中的信息進行比較。然後我使用if語句來進行比較。如果匹配,那麼程序會將用戶重定向到另一個類或方法,否則它不會執行任何操作。循環與hasNext方法作爲條件不斷拋出ArrayOutOfBounds異常? (Java)
問題是我一直拋出一個ArrayOutOfBounds異常作爲SOON,因爲它試圖從文件中讀取第一個行。
我扔了一個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();
}
}
}
}
我懷疑你的狀態是異常,因爲你在它上面的行中訪問該索引。沒有意識到明顯的錯誤(例如,「我」總是0)。你可以給我們一個自包含的代碼片段,實際上證明了這個問題嗎?你給我們的是一個粗略的副本,不能忠實地複製你的代碼。 –
是否正確聲明並初始化了數組'userLogin []'? –
你在哪裏填充userLogin []數組,它多長時間? –