为什么在 Java 中解析 CSV 电子表格会抛出 NumberFormatException?

Why is parsing CSV spreadsheet in Java throwing a NumberFormatException?

我正在尝试读取逗号分隔值文件,但我的代码似乎无法正常工作。有没有我的代码不正确的线索?

注意:尝试只读取 Price 列并确保值不为 NULL、N/A、小于 80 且大于 130。

电子表格:

boolean flag = true; 
File inputF = new File("data.csv")
InputStream getStream = new FileInputStream(inputF);

try{
    if(getStream.read() != -1){
    BufferedReader reader = new BufferedReader(new InputStreamReader(getStream));
    String line;

    while((line = reader.readLine()) != null){
        String[] csvs = line.split(",");
        for (String str : csvs){
            if (str != null && (str.equals("NA") || str.length() == 0)) {
                System.out.println(str);
                flag = false;
                break;
            }
        }
        if (!flag){
            break;
        }else{
            String Price = csvs[1]; 
            price = price.trim();
            Integer priceValue = new Integer(Price); //exception occurs here 
            if (priceValue != null && (priceValue < 80 || priceValue > 130)){
                flag = true;
            }
        }
    }
    reader.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println(flag);
return flag;

错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "PRICE" 

预期输出:(基于提供的电子表格)

True 

I am trying to read an Excel file ....

代码...和输出...表明您实际上试图解析 CSV 文件,而不是 Excel 文件。

异常信息是这样的:

Exception in thread "main" java.lang.NumberFormatException:
     For input string: "PRICE"

错误信息告诉我们。 “PRICE”字符串来自 CSV 文件的第一行。解决方法很简单。读取并丢弃文件的第一行;即包含列名而不是数据的行。

“价格”第一行出现错误。 仅检查数字:

    String price = csvs[1].trim();
    if (price.matches("-?\d+")) { // Possibly negative integer.
        int priceValue = Integer.parseInt(Price);
        if (priceValue < 80 || priceValue > 130){
            flag = true;
        }
    }