2012-12-06 27 views
1

我正在使用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(); 
    } 
} 

} 








} 
+1

我懷疑你的狀態是異常,因爲你在它上面的行中訪問該索引。沒有意識到明顯的錯誤(例如,「我」總是0)。你可以給我們一個自包含的代碼片段,實際上證明了這個問題嗎?你給我們的是一個粗略的副本,不能忠實地複製你的代碼。 –

+0

是否正確聲明並初始化了數組'userLogin []'? –

+0

你在哪裏填充userLogin []數組,它多長時間? –

回答

0

如果您嘗試更改行來閱讀System.out.println(this.userLogin[i]);?我的猜測是你有兩個名爲userLogin的變量,其中一個是本地函數(另一個是實例變量)。 this.userLoginuserLogin不一樣。道歉,如果我關閉。沒有違法意圖。

0

至於我可以告訴你設置用戶登陸到一個零長度陣列和永遠不會增加它:

private String userLogin[] = {}; 

因此訪問用戶登陸的任何元素應該拋出一個異常。這與inputFile.next()呼叫沒有任何關係。你可以做一個孤立的userLogin[0] = null它應該炸燬。

我建議你使用一個ArrayList或其他動態大小的結構,你可以建立增量。

0

將這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; 
     } 
    } 
} 
0

首先,你不需要掃描儀。 BufferedReader更簡單。你可以使用split來劃分你的單詞。另外,請注意流關閉的位置(在結尾處)。此外,我已經要求使用等於而不是==。最後,一個String.valueOf允許char數組與String進行比較。

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();