嘿,有没有人能帮我纠正我的错误。我正在拆分我的文件中的行,并检查它们是低、中还是高。如果字符串为空,我想读取文件中的下一行。我认为错误是在我解析了一个双精度值的时候。这是我的代码,任何帮助都是有用的!首先是我的错误
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at BSCQueryManager.displayBar(BSCQueryManager.java:431)
at BSCQueryManager.main(BSCQueryManager.java:57)以下是我的代码
String vMag;
String data;
double v;
int highCount = 0;
int medCount = 0;
int lowCount = 0;
// read file
File inFile = new File("bsc.dat");
Scanner starFile = new Scanner(inFile);
// while there is a vmag
while(starFile.hasNext()){
// read next line
data = starFile.nextLine();
data = data.substring(102, 107);
data.trim();
// if no vmag read next line
if(data.trim()!= ""){
v = Double.parseDouble(data);
// if vmag is > 6.0 add to countHigh
if (v > 6){
highCount++;
}
// if vmag is 5-6 add to countMed
if (v >= 5 && v <= 6){
medCount++;
}
// if vmag is < 5 add to countLow
if (v < 5){
lowCount++;
}
// end if
}
// end while
}
// display label
System.out.println(label);
System.out.println(highCount);
System.out.println(lowCount);
System.out.println(medCount);发布于 2013-04-01 07:54:24
尝试替换:
data.trim();
// if no vmag read next line
if(data.trim()!= ""){使用
data = data.trim();
// if no vmag read next line
if(data.length > 0){发布于 2013-04-01 07:58:05
其中一个字符串为空:
class Test1 {
public static void main(String[] args) {
Double.parseDouble("");
}
}产生:
C:\Temp>java Test1
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
at java.lang.Double.parseDouble(Double.java:510)
at Test1.main(Test1.java:3)发布于 2013-04-01 07:50:26
数据字符串肯定不能解析为双精度。可能为空(您的字符串非空逻辑有缺陷,请尝试
data.isEmpty() 取而代之)或者值可以是其他无法解析的东西,例如字母或单词。
您可以尝试调试,或者捕获异常并打印数据值。
try{
Double.parseDouble(data);
}catch(NumberFormatException e){
throw new RuntimeException(data + " is not a number");
}这会让你看到哪里出了问题。
https://stackoverflow.com/questions/15735882
复制相似问题