简体   繁体   中英

Getting array index out of bounds exception but don't know why?

I am trying to create a split() method that reads a file, line by line, then separates the String s and Integer s into an array that is then written to another file. I have created an array to hold each String called list and one to hold the Integer s called scores .

When I try to run my code, it reaches the second element in the list array which is surname = list[1] and then I get the error.

What I am trying to eventually do is split each element and get the average of the Integers so the original line of text would read Mildred Bush 45 65 45 67 65 and my new line of text would read Bush, Mildred: Final score is x.xx .

The error happens at line 7 surname = list[1];

My code:

public void splitTest()
{
    String forename, surname, tempStr, InputFileName, OutputFileName;
    tempStr = "";
    String[] list = new String[6];
    list = tempStr.split(" ");
    forename = list[0];
    surname = list[1];
    int[] scores = new int[5];
    scores[0] = Integer.parseInt(list[2]);
    scores[1] = Integer.parseInt(list[3]);
    scores[2] = Integer.parseInt(list[4]);
    scores[3] = Integer.parseInt(list[5]);
    scores[4] = Integer.parseInt(list[6]);
    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    FileOutputStream outputStream = null;
    PrintWriter printWriter = null;
    clrscr();
    System.out.println("Please enter the name of the file that is to be READ (e.g. details.txt) : ");
    InputFileName = Genio.getString();
    System.out.println("Please enter the name of the file that is to be WRITTEN TO (e.g. newDetails.txt) : ");
    OutputFileName = Genio.getString();
    try {
        fileReader = new FileReader(InputFileName);
        bufferedReader = new BufferedReader(fileReader); 
        outputStream = new FileOutputStream(OutputFileName);
        printWriter = new PrintWriter(outputStream);
        tempStr = bufferedReader.readLine();
        while (tempStr != null) {
            System.out.println(tempStr);
            printWriter.write(tempStr+"\n");
            tempStr = bufferedReader.readLine();
        }
        System.out.println("\n\nYOUR NEW FILE DATA IS DISPLAYED ABOVE!\n\n");
        pressKey();
        bufferedReader.close();
        printWriter.close();

    } catch (IOException e) {
        System.out.println("Sorry, there has been a problem opening or reading from the file");
        e.printStackTrace();
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();    
            } catch (IOException e) {
                System.out.println("An error occurred when attempting to close the file");
            }
        }
        if(printWriter != null) {
            printWriter.close();
        }
    }
}

Where it says Genio , this a class that deals with user input.

You initialize the list array with

list = tempStr.split(" ");

The size of that array can be anything. It depends on the number of spaces in tempStr . You have to check the length of list before accessing its elements.

If you expect the input to contain a certain number of parameters, add a check :

list = tempStr.split(" ");
int[] scores = new int[5];
if (list.length > 6) {
    forename = list[0];
    surname = list[1];       
    scores[0] = Integer.parseInt(list[2]);
    scores[1] = Integer.parseInt(list[3]);
    scores[2] = Integer.parseInt(list[4]);
    scores[3] = Integer.parseInt(list[5]);
    scores[4] = Integer.parseInt(list[6]);
}

This would prevent the exception. Of course, you have to decide how to handle the situation in which the condition is false.

EDIT :

tempStr = "";

This means there would be exactly one element in list . I'm assuming you meant to put some actual value in this variable.

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