在csv文件java中的现有数据下方追加数据

kg7wmglp  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(478)

我有一个csv文件,其中包含14列和5行。我正在读取最后一列的内容,对列值执行一些数学运算,并将其保存在名为 exposureFactor[] . 现在,我必须在第一列下面按列编写数组,跳过一行,这已经完成得很完美了。但是当我再次运行程序时,它会给我这个错误。

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at java.base/java.util.stream.ReferencePipeline$4$1.accept(ReferencePipeline.java:212)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:550)
at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
at java.base/java.util.stream.IntPipeline.toArray(IntPipeline.java:538)
at com.sajood.Readingcsv.appendCol.main(appendCol.java:28)

有人能强调一下这个问题吗。这是一个给我的项目,它的最后期限是在2天。

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

public class appendCol {

public static void main(String[] args) throws IOException {
    int sum = 0;
    String[] data5;
    CSVReader reader5 = new CSVReader(new FileReader("D:\\new.csv"));
    String[] nextLine5;
    List<String> li = new ArrayList<String>();
    while (null != (nextLine5 = reader5.readNext())) {
        li.add(nextLine5[nextLine5.length - 1]);

    }
    String[] tempe = li.toArray(new String[0]);
    String[] exp = Arrays.copyOfRange(tempe, 1, tempe.length);
    data5 = Arrays.toString(exp).replace("[", "").replace("]", "").replace("\"", "").replace(" ", "").split(",");
    int[] weightage = Arrays.stream(data5).mapToInt(Integer::parseInt).toArray(); 
    for (int i = 0; i < weightage.length; i++) {
        sum += weightage[i];
    }
    int average = sum / 6;
    String averageStr = String.valueOf(average);
    String exposureFactor[] = { "Exposure Factor", averageStr };
    FileWriter writer4 = new FileWriter("D:\\new.csv", true);
    CSVReader reader3 = new CSVReader(new FileReader("D:\\new.csv"), '\t', '\'', 6);
    if ((reader3.readNext()) != null) {
        return;
     } else {
        for (int j = 0; j < exposureFactor.length; j++) {
            if (j == 0) {
                writer4.append("\n");
            }
            writer4.append(String.valueOf(exposureFactor[j]));
            writer4.append("\n");
        }
        writer4.close();
    }
}

}

5sxhfpxr

5sxhfpxr1#

唯一可能的路线是: int[] weightage = Arrays.stream(data5).mapToInt(Integer::parseInt).toArray(); 我想它是想把你的空行解析成数字。为什么在输出曝光系数后需要再次运行它?
如果你的老板真的需要连续运行两次,那么快速解决方法就是添加一个try-catch,让它“优雅地”退出

try {
    int[] weightage = Arrays.stream(data5).mapToInt(Integer::parseInt).toArray();
} catch (NumberFormatException e) {
    System.out.println("Program has already been run.");
    System.exit(0);
}

你也可以用这个 if ((reader3.readNext()) != null) 检查你是否想返工。
还有一件事:你可能想要 average 当家 double 而不是 int .

相关问题