嗨,这是我在这里的第一个问题,所以如果由于某种原因不遵守规则,结果是重复的或其他什么,请告诉我(不是我一开始就失去任何声誉)
无论如何,我实际上对 Java 提供的此类 StringReader 有 2 个问题。首先,StringReader.ready() 到底是做什么的?我可以将它用作 while 循环中的条件,以便在字符串结束时终止循环吗?阅读 java 文档并没有太大帮助(或者我可能误解了他们所说的“如果保证下一个 read() 不会阻塞输入,则返回 True”的意思)
更新:
很抱歉,我错过了read()
字符串结束时返回 -1 的部分。无论如何,我的问题仍然是 ready() 部分。我认为这应该是检查字符串是否结束的那个?
任何帮助将不胜感激,谢谢!
导致问题的片段:
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);
}