Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
www - 02 May 2007 14:04 GMT
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.
Gordon Beaton - 02 May 2007 14:04 GMT
> 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

--
www - 02 May 2007 14:25 GMT
> Separate the trim() from the valueOf(), and use the default value when
> trim() results in an empty string:
[quoted text clipped - 16 lines]
>
> /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.
Gordon Beaton - 02 May 2007 14:37 GMT
> 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

--
Patricia Shanahan - 02 May 2007 15:30 GMT
>> Separate the trim() from the valueOf(), and use the default value when
>> trim() results in an empty string:
[quoted text clipped - 20 lines]
> 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
Tor Iver Wilhelmsen - 02 May 2007 20:54 GMT
På Wed, 02 May 2007 15:25:13 +0200, skrev www <www@nospam.com>:

> 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".


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.