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

huangapple go评论94阅读模式
英文:

Why is parsing CSV spreadsheet in Java throwing a NumberFormatException?

问题

以下是翻译好的内容:

我正在尝试读取一个逗号分隔的值文件,但似乎无法使我的代码工作。任何线索表明我的代码有错误吗?

备注:试图仅读取Price列,并确保值不为NULL,N/A,小于80且大于130。

电子表格:

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

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); //在这里发生异常
            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 a comma-separated value file but I can't seem to get my code to work. Any clue where my code is incorrect?

Notes: Trying to read only the Price column and make sure the values are not NULL, N/A, less than 80 and greater than 130.

SpreadSheet:

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

boolean flag = true; 
File inputF = new File(&quot;data.csv&quot;)
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(&quot;,&quot;);
        for (String str : csvs){
            if (str != null &amp;&amp; (str.equals(&quot;NA&quot;) || 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 &amp;&amp; (priceValue &lt; 80 || priceValue &gt; 130)){
                flag = true;
            }
        }
    }
    reader.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println(flag);
return flag;

Error:

Exception in thread &quot;main&quot; java.lang.NumberFormatException: For input string: &quot;PRICE&quot; 

Expected Output: (Based on spreadsheet provided)

True 

答案1

得分: 4

> 我正在尝试读取一个Excel文件....

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

异常信息如下:

在线程"main"中的异常java.lang.NumberFormatException:
     对于输入字符串:“PRICE”

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

英文:

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

The code ... and the output ... indicates that you are actually trying to parse a CSV file, not an Excel file.

The exception message is this:

Exception in thread &quot;main&quot; java.lang.NumberFormatException:
     For input string: &quot;PRICE&quot;

The error message is telling. That "PRICE" string is coming from the first line of your CSV file. The solution is simple. Read and discard the first line of the file; i.e. the line that contains column names not data.

答案2

得分: 0

错误出现在“PRICE。”的第一行。
仅检查数字:

    String price = csvs[1].trim();
    if (price.matches("-?\\d+")) { // 可能为负整数。
        int priceValue = Integer.parseInt(Price);
        if (priceValue < 80 || priceValue > 130){
            flag = true;
        }
    }
英文:

The error is given for the first line on "PRICE."
Check for digits only:

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

huangapple
  • 本文由 发表于 2020年9月4日 22:36:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63743220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定