按特定顺序将多个Arraylists中的元素写入txt文件

时间:2016-04-14 17:17:24

标签: java arraylist

执行作业并尝试编写按钮代码,以txt文件的形式生成报告。我为报告中所需的每条信息制作了ArrayLists,但没有设置初始大小。我有这段代码,我已经写过,但我对Java知之甚少。

    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错误,无法找出原因。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

替换此行:

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

使用<代替<=,如下所示:

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