0

For my computer science class I'm making an website username/password program. I decided to use a 2D string array, and it hasn't been working out the best. I tried to make a file reader to read the logins that get written but I keep getting the ArrayIndexOutOfBoundsException error. My file reader code is below, and also included is my login input code. I am just starting Java so I have very basic programming knowledge.

private void fileReader() throws FileNotFoundException {

    File inFile = new File(filePath);


    try {

        Scanner freader = new Scanner(inFile);

        while (freader.hasNextLine()) {

            for (int j = 1; j <= pass.length; j++) {

                pass[j][0] = freader.nextLine();

                pass[j][1] = freader.nextLine();

                pass[j][2] = freader.nextLine();

            }
        }

        freader.close();

    } catch (IOException e) {

        System.err.println(e);
        System.exit(1);

    }

Login input:

private void input() throws InterruptedException, FileNotFoundException {



    for (int i = 0; i < pass.length; i ++){

            System.out.println(i);

            if (i == pass.length){
                add();
            }

            c.print("Please enter the website: ");

            pass[i][0] = c.readLine();

            c.print("Please enter your username: ");

            pass[i][1] = c.readLine();

            c.print("Please enter your password: ");

            pass[i][2] = c.readLine();

            while (true){

                c.clear();
                    synchronized (c) {
                        c.println("To continue adding logins, press C. To exit the program press ESC.");
                        c.println(pass[i][0] + " " +  pass[i][1] + " " +  pass[i][2]);
                    }
                if (c.isKeyDown(KeyEvent.VK_C)){
                    break;
                }
                else if (c.isKeyDown(KeyEvent.VK_ESCAPE)){
                    fileWriter();
                    pass();
                }

                Thread.sleep(10);

            }

    }




}

Any help is greatly appreciated!! Thanks!

6
  • Where did you initialize your 2D array ? Commented May 17, 2016 at 23:57
  • Where is the ArrayIndexOutOfBoundsException ? In what line ? What is pass? How big is it ? What type is it ? Commented May 17, 2016 at 23:58
  • @UDKOX it starts off being 1 line, but increases by 1 each time the user adds a new login. Commented May 18, 2016 at 0:00
  • @T.Claverie I initialize it in the declaration, private String pass[][] = new String[1][3]; Commented May 18, 2016 at 0:00
  • for (int j = 1; j <= pass.length; j++) I think this is wrong, try for (int j = 0; j <= pass.length; j++). 0 instead of 1. Commented May 18, 2016 at 0:02

1 Answer 1

1

The main reason is because of this:

for (int j = 1; j <= pass.length; j++) { pass[j][0] = freader.nextLine(); pass[j][1] = freader.nextLine(); pass[j][2] = freader.nextLine(); }

An array starts from 0. By making j = 1, you are starting on the second array in the group, you need to start with 0 and read up to but not including the array length.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops, I forgot I had it at 1. I was trying all sorts of things to try and fix it. I changed it to for (int j = 0; j < pass.length; j++) { pass[j][0] = freader.nextLine(); System.out.println(pass[j][0]); pass[j][1] = freader.nextLine(); System.out.println(pass[j][1]); pass[j][2] = freader.nextLine(); System.out.println(pass[j][2]); } (dont mine the print, that was just to make sure it was printing the right values)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.