1

I am entirely new to Java with some background in C#. I am currently using Eclipse to run a method in a program to compare the method's arguments with usernames and passwords from a text file.

However, after running the code, I received an error called "java.lang.ArrayIndexOutOfBoundsException = 1". I do not understand why it has happened. Below contains the code and the file. I am trying to read only the three usernames and passwords on top.The other three below is just populating it and using later in the program.

**Update: Okay. I already fixed in changing the token into token[0] and token1. And it still doesn't work.

**Update no. 2: Also, this is the code that contains when I call the method. Below:

 s.login("tomrichards", "96744213");


public boolean login (String user, String password) {

  FileReader fr = new FileReader ("auth.txt");
  LineNumberReader r = new LineNumberReader (fr);

  String I = "";
  boolean identity = false;
  String name = "";
  String password = "";

  while ((I = r.readLine()) != null) {

    String [] token = I.split(";");

    name = token[0];
    pass = token[1];

    if (name.equals(user) && pass.equals(password)) {

      identity = true;

      System.out.println("Username " + name + "Password " + pass);

      return true;
    }

    else {

      identity = false;

      return false; 
    }

  }

   r.close();
}

Java Error

text file

**Update no.3: I double checked to see if there was any missing ";" in the text file, and there was none.

LSRain
  • 103
  • 1
  • 14
  • 3
    Indices are 0 based. Did you mean to use `token[0]` and `token[1]`? Also, you might have had a line in the file that does not have a `;`. – Sweeper Sep 28 '17 at 05:56
  • Did you run your code in the debugger, and look at the size of the array each time, or at least when you got the exception? If you're not familiar with debuggers, now is a very good time to learn. – Jon Skeet Sep 28 '17 at 05:57

3 Answers3

4

Your text file has empty lines

If I does not have any ; to split on then you would have to consider the size of the resulting array.

Also take care with the knowledge that Java arrays are indexed from 0

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • I see. If it's like that, then do I have to eliminate the empty line between the usernames & passwords and the three accounts? Or is it something else instead? – LSRain Sep 28 '17 at 06:09
  • You can test the length of the String or test if see if it contains the char that you want to split on – Scary Wombat Sep 28 '17 at 06:10
  • Right. Because I am trying to split only the char between the username & password only. – LSRain Sep 28 '17 at 06:15
0

Consider a case when a line in your input doesn't have a ; to split.

name = token[1]; // there is no value to be accessed in the token[]

Hence this results into the ArrayIndexOutOfBoundsException.

Naman
  • 27,789
  • 26
  • 218
  • 353
0

First of all, this looks weird:

name = token[1];
pass = token[2];

I think you mean:

name = token[0];
pass = token[1];

unless the first thing before ; in the text file is something you discard.

Secondly, your text file seems to at least have a line that does not have a ;. Search for that and correct it. If you can't find it, you can fix this error temporarily by doing:

String [] token = I.split(";");

if (token.length != 2) {
    continue;
}

name = token[0];
pass = token[1];

I said temporarily since this fix is not really fixing the root of the problem, but just avoiding to read invalid lines.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
Sweeper
  • 213,210
  • 22
  • 193
  • 313