gpt4 book ai didi

Java字符串解析错误

转载 作者:行者123 更新时间:2023-12-01 04:46:33 26 4
gpt4 key购买 nike

嘿有人可以帮我解决我遇到的错误吗?我正在从文件中取出行并检查它们是低中还是高。如果字符串为空,我想读取文件中的下一行。我认为错误是当我将刺痛解析为 double 时。这是我的代码,如有任何帮助,请采纳! 首先这是我的错误

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);

最佳答案

尝试替换:

data.trim();
// if no vmag read next line
if(data.trim()!= ""){

data = data.trim();
// if no vmag read next line
if(data.length > 0){

关于Java字符串解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15735882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com