|
Val's Blog
Lots of stuff for Web 2.0 freaks and Javaholics
|
|
|
Alan M. Davis: "If you believe that you know the requirements better than the customer, you are part of the problem, not the solution." |
[ Login ] |
|
A very common mistake that might seem obvious (or so I hope), yet that I've seen in many places is the one where we need to read characters from any kind of input streams and we cast an int read from that input stream to a char a little bit too early. The usual code goes like this:
InputStream in = ...;
char c = 0;
while ((c = (char) in.read()) > -1) {
//do something with c
}
The
The reason for this is that the type
InputStream in = ...;
int i = 0;
while ((i = in.read()) > -1) {
char c = (char) i;
//do something with c
}
//EOF reached
That way the loop will always break at some point and you'll be safe.
TrackBacks[0]
Comments[0]
Posted by val on May 14, 2008 2:36:12 PM CEST
TrackBack to http://radio.javaranch.com/val/addTrackBack.action?entry=1210768572268
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Content © Val | Powered by Pebble 1.9.1 |