Detecting eof

J

jack.smith.sam

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
 
L

Lionel

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

rr news

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());
}


--
---------------------
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
 
L

Lionel

rr said:
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());
}

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

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Lionel said:
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.

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

Arne
 
H

hiwa

Lionel ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
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.
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.
 
G

Guest

hiwa said:
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.

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

Arne
 
H

hiwa

Arne Vajhøj ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
And your solution is different regarding readLine block
or exception how ??

Arne
Oh no. Data canonicalization is often much more important than your
code issue.
I call them 'data bug' issue.
 
K

Karl Uppiano

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

Why not unconditionally spin in a loop and catch the EOFException to break
out?
 
H

hiwa

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

EJP

Karl said:
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.
 
D

Dale King

Lionel said:
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.
 
L

Lionel

EJP said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top