Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to deal with empty string when reading input from a file?

Reply
Thread Tools

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

 
 
Gordon Beaton
Guest
Posts: n/a
 
      05-02-2007
On Wed, 02 May 2007 09:04:15 -0400, www wrote:
> 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

--
 
Reply With Quote
 
 
 
 
www
Guest
Posts: n/a
 
      05-02-2007
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.
 
Reply With Quote
 
 
 
 
www
Guest
Posts: n/a
 
      05-02-2007
Gordon Beaton wrote:

>
> 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.
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      05-02-2007
On Wed, 02 May 2007 09:25:13 -0400, www wrote:
> 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

--
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      05-02-2007
www wrote:
> Gordon Beaton wrote:
>
>>
>> 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.


The simple answer is just that those are the rules - see the
Double.valueOf documentation,
http://java.sun.com/j2se/1.5.0/docs/...va.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
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      05-02-2007
På Wed, 02 May 2007 15:25:13 +0200, skrev www <(E-Mail Removed)>:

> 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".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
AMD64 or Semperon, deal or no deal? Tad Confused Computer Information 7 04-13-2006 05:43 PM
deal or no deal rbt Python 7 12-28-2005 08:57 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
Input Drops With An Empty Input Queue Spiz Cisco 12 05-18-2005 05:28 PM



Advertisments