Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Asin
Gerbil Team Leader
Topic Author
Posts: 292
Joined: Tue Mar 09, 2004 10:36 pm
Location: Ontario, Canada
Contact:

Catching EOFExceptions in Java

Sat Nov 12, 2005 9:36 pm

This is essentially the code that I have:

try {
    this.curSearchString = this.in.readLine ();
   
    while (true) {
        if (this.curSearchString == null) {
            // if there is a blank line, skip the search string
            continue;
        } // end if

        // ...

        curLine++;
        curSearchString = this.in.readLine ();
    } // end while
} catch (EOFException eof) {
    // end-of-file reached
    // do nothing
} catch (IOException e) {
    System.err.println ("Error (MyGrep.search) :  " + e.toString ());
    e.printStackTrace ();
} // end try-catch block


For some reason, it's looping infinitely despite an 18 line input file. It worked without the end of file check, but that was before I was told that blanklines are fair game for this assignment.

this.in is a BufferedReader (FileReader) object.

Any help is appreciated.
 
UberGerbil
Grand Admiral Gerbil
Posts: 10368
Joined: Thu Jun 19, 2003 3:11 pm

Sat Nov 12, 2005 9:58 pm

I'm not a java programmer, but are you sure that it throws an exception for EOF? In most languages and libraries I've used, EOF is not an error condition. The documentation appears to suggest it merely returns a null string (and EOFException wouldn't occur, as it is only used for "unexpected" EOFs which this wouldn't be). Since you're interpreting a null string as a blank line...
 
Asin
Gerbil Team Leader
Topic Author
Posts: 292
Joined: Tue Mar 09, 2004 10:36 pm
Location: Ontario, Canada
Contact:

Sat Nov 12, 2005 10:05 pm

Oh, that's interesting. Apparently it does work.

I just did a quick modification and trace of my program and it looks like blank lines are just 0 length strings.

Thanks for the help.
 
Flying Fox
Gerbil God
Posts: 25690
Joined: Mon May 24, 2004 2:19 am
Contact:

Sun Nov 13, 2005 12:11 am

How many more active CPUs for Folding are we going to get as a result of this assignment help?

Who is online

Users browsing this forum: No registered users and 19 guests
GZIP: On