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 / First Aid / August 2006

Tip: Looking for answers? Try searching our database.

BufferedReader readLine returning NoSuchElementException

Thread view: 
Tom Peel - 15 Aug 2006 17:01 GMT
I'm using BufferedReader and readLine to read a textfile into a program.
The lines are terminated by CRLF strings.
The problem is that there are some empty lines, that is, there are
sequences of CRLFCRLF without any interverning characters. The readLine
throws a NoSuchElementException, and never reaches the end of the file.

How can I get readLine to read the whole file and terminate at the end?

Here is some test code:

        try {
            int i = 0;
            File f = new File(args[i]);
            System.out.println("Begin Processing " + f.toString() + " ..."  +
f.toURL() + " ...");   
            br = new BufferedReader(new FileReader(f));
            System.out.println("Now Processing " +   " ...");   

            try {
                 while ((line=br.readLine())!=null) {  // lines are terminated by CRLF
                    System.out.println("Line " +   " ..." + line);   
                    processSCALine(line);
                }    // while
            }
            catch (Exception e) {
                System.out.println("Exception reading: " + e.toString() + " Line " +
  " ..." + line);       
            }
            finally {
                br.close();
            }
        }
        catch (Exception e) {
            System.out.println("Exception opening: " + e.toString());
            return;
        }
}

Signature

In memorium Layal Najib
www.cpj.org

Eric Sosman - 15 Aug 2006 17:22 GMT
Tom Peel wrote On 08/15/06 12:01,:
> I'm using BufferedReader and readLine to read a textfile into a program.
> The lines are terminated by CRLF strings.
> The problem is that there are some empty lines, that is, there are
> sequences of CRLFCRLF without any interverning characters. The readLine
> throws a NoSuchElementException, and never reaches the end of the file.
> [...]

   Since your code catches the exception and doesn't print
the stack trace, how do you know the exception is thrown by
readLine() and not by processSCALine()?

Signature

Eric.Sosman@sun.com

Tom Peel - 16 Aug 2006 10:21 GMT
Eric Sosman schrieb:

> Tom Peel wrote On 08/15/06 12:01,:
>> I'm using BufferedReader and readLine to read a textfile into a program.
[quoted text clipped - 7 lines]
> the stack trace, how do you know the exception is thrown by
> readLine() and not by processSCALine()?

Eric (and Daniel)

You are both quite correct, the exception is being thrown in
processSCALine. Thanks for the help pointing out my error.

T.
dsjoblom@abo.fi - 15 Aug 2006 17:22 GMT
> I'm using BufferedReader and readLine to read a textfile into a program.
> The lines are terminated by CRLF strings.
> The problem is that there are some empty lines, that is, there are
> sequences of CRLFCRLF without any interverning characters. The readLine
> throws a NoSuchElementException, and never reaches the end of the file.

Huh? No it doesn't, or if it really does, you have a broken Java
implementation (not likely). Post the whole stacktrace for the
exception. The exception almost certainly occurs in your
processSCALine(line); method.

Regards,
Daniel Sjöblom


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



©2008 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.