Hi this is my first question here so if it is for some reason does not obey the rules, turns out to be a duplicate or whatever, please tell me kindly (not that I have any reputation to lose in the first place)
Anyway, I actually have sort of 2 questions about this class StringReader that Java provides. First, what exactly the StringReader.ready() do? Can I use it as a condition in a while loop so that the loop terminates when the string ends? Reading the java doc didn't help much (or maybe I misunderstood what they meant by "Returns True if the next read() is guaranteed not to block for input")
Update:
Sorry apparently I missed the part where it says read()
returns -1 when the string ends. Anyway, my question remains for the ready() part. I thought this is supposed to be the one that checks if the string has ended?
Any help would be appreciated, thanks!
Link to the actual source code
The snippet that causes the problem:
while (text.ready()) {
// Updating stringBuffer, using some sort of 'rolling hash' (or is
// it
// indeed rolling hash?)
stringBuffer.deleteCharAt(0);
stringBuffer.append((char) next);
// The next character that follows the sequence of k characters
next = text.read();
// store the string form of the buffer to avoid rebuilding the
// string for the next few checks
String key = stringBuffer.toString();
if (hashmap.containsKey(key)) {
// If the hash map already contain the key, retrieve the array
asciiArray = hashmap.get(key);
} else {
// Else, create a new one
asciiArray = new int[128];
}
System.out.println(next);
// Error checking on my side only, because some of the text sample I
// used contains some characters that is outside the 128 ASCII
// character, for whatever reason
if (next > 127) {
continue;
}
// Increment the appropriate character in the array
asciiArray[next]++;
// Put into the hash map
hashmap.put(key, asciiArray);
}