João Matos João Matos - 5 months ago 37
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?



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.