Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
In particular, comparing a value of type {{char}} with -1 will never yield {{true}}.  However, because the method {{read()}} returns -1 to indicate {{EOF}}, it is tempting to try to compare the character returned by {{read()}} with -1.  This is a common error \[[Pugh 08|AA. Java References#Pugh 08]\].

...

In this noncompliant code, the value of type int returned by the read() method is cast directly to a char, and that value of type char which is compared with -1 to try to detect EOF. This conversion leaves the value of c as 0xffff (Character.MAX_VALUE) instead of -1. Hence, this test never evaluates to true.

...

Always use a signed type of sufficient size to store signed data. To be compliant, use an a value of type int type to check for EOF while reading in data. If the value of type int value returned by read() is not -1, then it can be safely cast to a value of type char.

Code Block
bgColor#ccccff
int c;
while ((c=in.read())!= -1) {ch = (char)c; ... }

...