简体   繁体   中英

Array index out of bounds exception in quiz program

I am having the hardest time trying to fix an ArrayIndexOutOfBoundsException .

I have a method that reads from a file line by line. If the name and id on the line matches some variables that I pass to the method then I save that line to an array.

This program simulates a quiz. The user cannot use the same name and id more than 2 times; therefore, the file only contains 2 lines with the same name and id.

I created an array named temp to hold those two lines from the file. If the file is empty the user takes his two tries and when he tries again it he is denied. So if you enter a different name and id you should get 2 more tries. At this point the file has two lines from the previous user, but when the new user tries he can only take the test once. When he tries the second time I get the array out of bounds exception.

My question is: Is the array temp holding the previous values and is that why I am getting the exception?

private String readFile(String id, String name) {
    String[] temp = new String[3];
    int i = 1;
    int index = 0;
    String[] split = null;
    String idCheck = null;
    String nameCheck = null;
    temp = null;

    try {
        BufferedReader read = new BufferedReader(new FileReader("studentInfo.txt"));
        String line = null;           

        try {
            while ((line = read.readLine()) != null) {
                try {
                    split = line.split("\t\t");
                } catch (Exception ex) {
                }

                nameCheck = split[0];
                idCheck = split[1];

                if (idCheck.equals(id) && nameCheck.equals(name)) {
                    temp[index] = line;
                }

                index++;
            }
            read.close();
        } catch (IOException ex) {
        }
    } catch (FileNotFoundException ex) {
    }

    if (temp != null) {
        if (temp[1] == null) {
            return temp[0];
        }
        if (temp[1] != null && temp[2] == null) {
            return temp[1];
        }
        if (temp[2] != null) {
            return temp[2];
        }
    }

    return null;
}

I see two places where you can get an index out of bounds exception. First is this code:

try {
    split = line.split("\t\t");
} catch (Exception ex) {
}
nameCheck = split[0];
idCheck = split[1];

If the line does not have a "\\t\\t" sequence, then split will have only one element and trying to access split[1] will throw an exception. (As an aside: you shouldn't silently ignore exceptions!)

The second (and more likely source of the problem) is that you are incrementing index for every line that has matching id and name, so once you read the third such line, index is out of bounds as a subscript for temp .

You can either include index < temp.length in your while loop condition, or you can use an ArrayList<String> for temp instead of a String[] . That way you can add an unlimited number of strings.

This is what may be happening

    String[] split = "xxx\tyyyy".split("\t\t");
    System.out.println(split[0]);
    System.out.println(split[1]);

.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Test.main(Test.java:17)

After setting temp = null;

The next reference to temp is:

if (idCheck.equals(id) && nameCheck.equals(name)) {

    temp[index] = line;
}

I believe you should remove the line temp = null; . All it does is trash the array you just instantiated above that line.

That index makes me a touch nervous, but I suppose if you are certain that the file being read will never have more than 3 lines...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM