Note that there are some explanatory texts on larger screens.

plurals
  1. POWhile loop with hasNext method as condition keeps throwing ArrayOutOfBounds Exception? (Java)
    primarykey
    data
    text
    <p>I'm working on a username/password login using GUI. I'm using a while loop with a hasNext() extension to the file name as the condition to store the information that's in the file into an array so that I can compare the user's input to the information that's stored in the file. Then I use an if statement to do the comparison. If there's match, then the program redirects the user to another class or method, otherwise it doesn't do anything. </p> <p>The problem is that I keep throwing an ArrayOutOfBounds exception as SOON as it tries to read the <em>first</em> line from the file.</p> <p>I threw in a System.out.println(userLogin[i]) to see if it looped all the way through, but it must not since the line never outputs. My guess is that it stops first thing once it gets into the while loop. How do I fix this? Your help is greatly appreciated. If you need me to clarify the question, please say so in your comment and I'll re-edit my post to be as clear as possible. This is my code as follows:</p> <pre><code> 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(); } } } </code></pre> <p>That was the class and method that is supposed to send over the user's input for username and password over to the method that checks if there's match to what's stored in the file.</p> <p>This is the method that verifies whether the input has a match:</p> <pre><code>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) &amp;&amp; (this.passwordCheckLogin == password)){ JOptionPane.showMessageDialog(null, "Access Granted"); inputFile.close(); break; } i++; } } </code></pre> <p>The information in the file is written in this manner (username on the left followed by the password on the right):</p> <pre><code>John001 bananas Mike001 123chocolate </code></pre> <p>Thank you! </p> <p>Here's the whole code, sorry for not including it earlier. Hopefully this helps you guys better understand my question. Thank you again for taking the time out to answer.</p> <pre><code>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) &amp;&amp; (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(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload