簡體   English   中英

使數組索引超出范圍異常,但不知道為什么?

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

我正在嘗試創建一個split()方法,該方法逐行讀取文件,然后將StringInteger分離成一個數組,然后將其寫入另一個文件。 我創建了一個數組來保存每個稱為list String並保存一個稱為scoresInteger

當我嘗試運行我的代碼時,它到達list數組中的第二個元素,即surname = list[1] ,然后出現錯誤。

我最終想要做的是拆分每個元素並獲取整數的平均值,這樣原始文本行將顯示為Mildred Bush 45 65 45 67 65而我的新文本行將顯示為Bush, Mildred: Final score is x.xx

錯誤發生在第7行, surname = list[1];

我的代碼:

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();
        }
    }
}

Genio ,這是一個處理用戶輸入的類。

您使用以下方法初始化list數組

list = tempStr.split(" ");

該數組的大小可以是任何值。 這取決於tempStr的空格tempStr 您必須在訪問list元素之前檢查list的長度。

如果您期望輸入包含一定數量的參數,請添加檢查:

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]);
}

這樣可以防止異常。 當然,您必須決定如何處理條件為假的情況。

編輯:

tempStr = "";

這意味着list中將只有一個元素。 我假設您要在此變量中放入一些實際值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM