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 / September 2006

Tip: Looking for answers? Try searching our database.

Detecting eof

Thread view: 
jack.smith.sam@gmail.com - 11 Sep 2006 00:58 GMT
Hi All,

I am using this segment to read a file:

String str
while ((str=in.readLine()!=eof)

but I get error because of eof (I want to detect end of file). How can
I use it?
Also is there any way to detect blank lines in a file? for example:
a

b
If I read the second line is it equal to null or something elase?

Thanks for your help
Jack
Lionel - 11 Sep 2006 01:05 GMT
> Hi All,
>
> I am using this segment to read a file:
>
> String str
> while ((str=in.readLine()!=eof)

What is /in/? BufferedReader? I suspect that you want to do:

while((str = in.readLine()) != null)

/in/ is not a very good variable name either.

> but I get error because of eof (I want to detect end of file). How can
> I use it?
> Also is there any way to detect blank lines in a file? for example:
> a
>
> b

Well str will equal the empty string "". Or, if the line is full of
spaces you can do str = str.stripWhiteSpace() to be sure that str is the
empty string. Then you do str.equals("") to find out if it is empty or not.

Lionel.
Dale King - 12 Sep 2006 13:48 GMT
>> Also is there any way to detect blank lines in a file? for example:
>
> Well str will equal the empty string "". Or, if the line is full of spaces
> you can do str = str.stripWhiteSpace() to be sure that str is the empty
> string. Then you do str.equals("") to find out if it is empty or not.

The method you wanted is called trim, which removes leading and trailing
whitespace from the string.
Signature

Dale King

rr news - 11 Sep 2006 01:09 GMT
Try this.

boolean eof;
eof = false;
try {
   while (! eof) {
       String str = in.readLine();
       if (str == null) {
           eof = true;
       } else {
           System.out.println(str);
       }
   }
} catch (Exception e) {
   System.err.println ("Error: " + e.getMessage());
}
   

Signature

---------------------
Jason Herald
www.cingular.com

Hi All,

I am using this segment to read a file:

String str
while ((str=in.readLine()!=eof)

but I get error because of eof (I want to detect end of file). How can
I use it?
Also is there any way to detect blank lines in a file? for example:
a

b
If I read the second line is it equal to null or something elase?

Thanks for your help
Jack
Lionel - 11 Sep 2006 01:34 GMT
> Try this.
>  
[quoted text clipped - 12 lines]
>     System.err.println ("Error: " + e.getMessage());
> }

That while loop seems a whole lot uglier than:

String str;

while((str = in.readLine()) != null) {
    System.out.println(str);
}

It's also easier to make a mistake in the body of the while-loop by
doing it that way.

Lionel.
Arne Vajhøj - 11 Sep 2006 01:43 GMT
>> boolean eof;
>> eof = false;
[quoted text clipped - 21 lines]
> It's also easier to make a mistake in the body of the while-loop by
> doing it that way.

I would say that the last code is *the standard* way of
doing it.

Arne
hiwa - 11 Sep 2006 01:54 GMT
Lionel のメッセージ:

> > Try this.
> >
[quoted text clipped - 25 lines]
>
> Lionel.
Yes, yours is the standard idiom for readLine() usage.
Beware, however, if the original resource has one or more non-canonical
line(s) which do not have a '\n' at the end, readLine() could block
forever or weird Exception could be thrown.
Arne Vajhøj - 11 Sep 2006 01:59 GMT
> Lionel のメッセージ:
>>> boolean eof;
[quoted text clipped - 11 lines]
>>>     System.err.println ("Error: " + e.getMessage());
>>> }

>> String str;
>>
>> while((str = in.readLine()) != null) {
>>      System.out.println(str);
>> }

> Yes, yours is the standard idiom for readLine() usage.
> Beware, however, if the original resource has one or more non-canonical
> line(s) which do not have a '\n' at the end, readLine() could block
> forever or weird Exception could be thrown.

And your solution is different regarding readLine block
or exception how ??

Arne
hiwa - 11 Sep 2006 03:31 GMT
Arne Vajhøj のメッセージ:

> > Lionel のメッセージ:
> >>> boolean eof;
[quoted text clipped - 27 lines]
>
> Arne
Oh no. Data canonicalization is often much more important than your
code issue.
I call them 'data bug' issue.
Karl Uppiano - 11 Sep 2006 07:41 GMT
> Hi All,
>
[quoted text clipped - 13 lines]
> Thanks for your help
> Jack

Why not unconditionally spin in a loop and catch the EOFException to break
out?
hiwa - 11 Sep 2006 07:45 GMT
Karl Uppiano のメッセージ:

> Why not unconditionally spin in a loop and catch the EOFException to break
> out?
That should only works on some networking situation. It doesn't have
generality.
EJP - 12 Sep 2006 08:24 GMT
> Why not unconditionally spin in a loop and catch the EOFException to break
> out?

Because it won't be thrown.

readLine() is defined to return null at EOF, not to throw an EOFException.
Lionel - 12 Sep 2006 23:47 GMT
>> Why not unconditionally spin in a loop and catch the EOFException to
>> break out?
>
> Because it won't be thrown.
>
> readLine() is defined to return null at EOF, not to throw an EOFException.

You could catch the NullPointerException :) . . . lol


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.