2016-04-14 74 views
-1

做了一項任務,我試圖編寫一個按鈕的代碼以txt文件的形式生成報告。我爲報告中所需的每條信息製作了ArrayLists,但尚未設置初始大小。我有這段代碼,我已經寫了,但我不是非常瞭解Java。按照特定順序將多個Arraylists中的元素寫入txt文件

File newTextFile = new File("D:/Report.txt"); 
      FileWriter fw = new FileWriter(newTextFile); 
      int i=0; 
      for(int i=0; i<=FirstName.size(); i++;){ 
       fw.write(FirstName.get(i)); 
       fw.write(LastName.get(i)); 
       fw.write(DOB.get(i)); 
       fw.write(Gender.get(i)); 
       fw.write(Email.get(i)); 
       fw.write(Address.get(i)); 
       fw.write(Number.get(i)); 
       fw.write(Attending.get(i)); 
      } 
      int x=0; 
      if(InternationalCheck.get(x) == "yes"){ 
       int y=0; 
       fw.write(Nationality.get(x)); 
       x++; 
      } 
      int DomesticPrice = 50; 
      int InternationalPrice = 150; 
      int sum = (DomesticCheck.size()*DomesticPrice)+(InternationalCheck.size()*InternationalPrice); 
      String sum1 = ""+sum; 
      String Total = "The total revenue is:"; 
      fw.write(Total); 
      fw.write(sum1); 
      fw.close(); 

我不斷收到IndexOutOfBoundsException錯誤,並找不到原因。任何幫助,將不勝感激

+0

請提供[最小,完整的,並且可驗證示例](HTTP ://stackoverflow.com/help/mcve)幷包含異常堆棧跟蹤。 – nyname00

回答

0

替換此行:

for(int i=0; i<=FirstName.size(); i++;){ 

隨着使用<代替<=象下面這樣:

for(int i=0; i<FirstName.size(); i++){ 
+0

非常感謝你不能相信這就是這麼簡單,我覺得很愚蠢 –