我知道有幾個關於這個特定錯誤的帖子,但是這些帖子的答案都沒有解決我的問題。基本上我正在讀取一個txt文件到列表列表中。它編譯沒有錯誤,但是當我運行它時,我得到「ArrayIndexOutOfBoundsException:2」。顯然這個問題在第359行是「double tempArea = Double.parseDouble(tokenize [2]);」。我無法看到該行的任何錯誤,所以我也檢查了額外的大括號或分號,但沒有找到。我在下面列出了我的代碼。任何人都可以看到錯誤的原因可能是什麼?數組索引越界
ArrayList<String>listS = new ArrayList<>();
try
{
BufferedReader bR = new BufferedReader (new FileReader("EssentialsSaleFile.txt"));
String fileRead = bR.readLine();
while(fileRead != null)
{
String[] tokenize = fileRead.split(",");
String tempType = tokenize[0];
String tempAddress = tokenize[1];
double tempArea = Double.parseDouble(tokenize[2]);
int tempNumOfBedrooms = Integer.parseInt(tokenize[3]);
int tempNumOfToilets = Integer.parseInt(tokenize[4]);
int tempNumOfGarages = Integer.parseInt(tokenize[5]);
String tempSellerName = tokenize[6];
String tempSellerPhone = tokenize[7];
String tempSaleOfferDate = tokenize[8];
String tempSaleOfferEndDate = tokenize[9];
double tempAskingAmount = Double.parseDouble(tokenize[10]);
String tempAssignedEmployee = tokenize[11];
double tempSalesCommissionPerc = Double.parseDouble(tokenize[12]);
String tempObj = new String();
listS.add(tempObj);
fileRead = bR.readLine();
}
bR.close();
}
catch(FileNotFoundException ex)
{
JOptionPane.showMessageDialog(null,"File not here.");
}
catch (IOException e)
{
}
嗯,你的文本文件中只有兩個元素的行?提示:嘗試在分割之前記錄你分割的行...... –
*顯然*'tokenize'只包含2個元素。這就是錯誤信息告訴你的,這是你必須研究的,在行前設置一個斷點並檢查'tokenize'和'fileRead'。 – luk2302