João Matos João Matos - 1 year ago 111
Java Question (Java 8) returns unexpected character at the end of the String

this problem causes an infinite loop in the following code excerpt:

public static final List<String> extractTags(String source, Integer nTags) {

List<String> tags = new ArrayList<>();

try (StringReader stringReader = new StringReader(source)) {
String tag = "";
char c;
while ((c = (char) >= 0 && tags.size() < nTags) {
switch (c) {
case '<':
tag = "";
case '>':
tag = tag + c;
} catch (IOException e) {
} finally {
return tags;

if invoked with the following parameters:
source = "trash"
nTags = 2

Using a debugger I realized that after the string was completely iterated, the read() method returns the char '\uFFFF' 65535 forever. So my question is why?


Answer Source

Because is returning -1 for end of stream, but you're casting it to char which is the only unsigned datatype in Java. So instead of -1 you're getting 65535 for end of stream therefore never breaking the while loop.

You might want to cast what is read into a char inside the loop, instead of at the while condition.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download