Hey can someone help me with my error i am getting. I am taking apart lines out of my file and check wether they are low med or high. If the string is blank i want to read the next line in the file. I think the error is when i am parsing the sting to a double. Heres my code any help is appriciated! First here is my error
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)
Here is my code
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);
Try replacing:
data.trim();
// if no vmag read next line
if(data.trim()!= ""){
with
data = data.trim();
// if no vmag read next line
if(data.length > 0){
One of the strings is empty:
class Test1 {
public static void main(String[] args) {
Double.parseDouble("");
}
}
produces:
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)
The data string definitely isn't parsable to a double. It may be blank (your string not empty logic is flawed, try
data.isEmpty()
instead) or the value could be something else unparseable eg a letter or word.
You could try debugging, or catching the exception and printing the value of data.
try{
Double.parseDouble(data);
}catch(NumberFormatException e){
throw new RuntimeException(data + " is not a number");
}
That will let you see what's going wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.