英文:
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); //在这里发生异常
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:
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;
Error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "PRICE"
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 "main" java.lang.NumberFormatException:
For input string: "PRICE"
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("-?\\d+")) { // Possibly negative integer.
int priceValue = Integer.parseInt(Price);
if (priceValue < 80 || priceValue > 130){
flag = true;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论