How to deal with empty string when reading input from a file?

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

--
 
W

www

Hi,

My program reads in a text file which has strict format. Briefly, my
program looks like:

BufferedReader inputStream = new BufferedReader(new
FileReader("input.txt"));

String line1 = inputStream.readLine();
String line2 = inputStream.readLine();

double value = Double.valueOf(line1.substring(0,25).trim());
double valueB = Double.valueOf(line1.substring(30,45).trim());
....

The number in input.txt located in the first line in the position from
1st space to 25th space will be assigned to the variable value, number
between position 31 and 45 will be assigned to valueB.

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.

Could you help me to modify my program to take care such a case? For
other reasons unspecified here, I cannot use String Tokenizer or Pattern
split method. I want to stick to the code above with some minor changes.

Thank you very much.
 
W

www

Gordon said:
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

Thank you. I was surprised that, why Double.valueOf("") is not 0.0?
instead it throws an Exception!
Could you help me a little more? Thank you.
 
G

Gordon Beaton

Thank you. I was surprised that, why Double.valueOf("") is not 0.0?
instead it throws an Exception!

At the risk of stating the obvious, the string "" does not represent
any number at all. You've chosen to interpret it as 0.0, but that's up
to you and not necessarily a universal interpretation. In many cases a
missing value is an error.

Double.valueOf() is defined to return a value according to the syntax
rules of the JLS. In the Java language you can't just leave out a
value when you want to specify 0, and the same applies to
Double.valueOf().

/gordon

--
 
P

Patricia Shanahan

www said:
Thank you. I was surprised that, why Double.valueOf("") is not 0.0?
instead it throws an Exception!
Could you help me a little more? Thank you.

The simple answer is just that those are the rules - see the
Double.valueOf documentation,
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#valueOf(java.lang.String)

However, you probably want to know why those are the rules.

I don't think there is any one-size-fits-all right answer to the
handling of an empty string, or an all space string. In that sort of
situation, it is usually better to err on the side of reporting the
problem, to bring it to the programmer's attention. The programmer can
then decide the right handling on a case-by-case basis.

Incidentally, Double.valueOf ignores leading and trailing whitespace, so
normally there is no need to trim. However, in this case it simplifies
the all space test.

Patricia
 
T

Tor Iver Wilhelmsen

Thank you. I was surprised that, why Double.valueOf("") is not 0.0?
instead it throws an Exception!
Could you help me a little more? Thank you.

What makes 0.0 a more likely answer to "what is the number represented by
nothing" than, say, not-a-number (Double.NaN)? Since someone would prefer
one and others the other, the library develoers chose to throw an
exception instead, in order to say "don't do silly stuff like that".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top