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

Discussion in 'Java' started by Gordon Beaton, May 2, 2007.

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

    --
     
    Gordon Beaton, May 2, 2007
    #1
    1. Advertising

  2. Gordon Beaton

    www Guest

    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.
     
    www, May 2, 2007
    #2
    1. Advertising

  3. Gordon Beaton

    www Guest

    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.
     
    www, May 2, 2007
    #3
  4. 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

    --
     
    Gordon Beaton, May 2, 2007
    #4
  5. 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/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
     
    Patricia Shanahan, May 2, 2007
    #5
  6. PÃ¥ Wed, 02 May 2007 15:25:13 +0200, skrev www <>:

    > 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".
     
    Tor Iver Wilhelmsen, May 2, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    398
    Elton Wang
    Feb 14, 2005
  2. Lukas
    Replies:
    3
    Views:
    815
    spiff
    Nov 10, 2005
  3. rbt

    deal or no deal

    rbt, Dec 22, 2005, in forum: Python
    Replies:
    7
    Views:
    552
    Duncan Smith
    Dec 28, 2005
  4. Tzury Bar Yochay
    Replies:
    1
    Views:
    406
    Gabriel Genellina
    Mar 24, 2008
  5. Replies:
    1
    Views:
    249
Loading...

Share This Page