G
Gordon Beaton
My problem is: with the above code, in input.txt first line, if
between position 1 to 25 is empty, I "wish" value be assigned
automatically as zero. But the program fails, with error saying
something like Wrong Format Exception "". Similarly, if position
between 31 and 45 is empty in the first line of input.txt, program
will also fail.
Separate the trim() from the valueOf(), and use the default value when
trim() results in an empty string:
double doubleOrZero(String s) throws NumberFormatException {
String trimmed = s.trim();
if ("".equals(trimmed)) {
return 0.0;
}
else {
return Double.valueOf(trimmed);
}
}
then:
double value = doubleOrZero(line1.substring(0,25));
double valueB = doubleOrZero(line1.substring(30,45));
/gordon
--