我不知道如何编写“下一个源字符”代码来查找字符串的下一个字符。
public class scanner {
protected char currentChar;
protected StringBuffer currentSpelling;
protected byte currentKind;
private void take (char expectedChar){
if (currentChar == expectedChar){
currentSpelling.append(currentChar);
currentChar = next source character;
}
}
private void takeIt(){
currentSpelling.append(currentChar);
currentChar = next source character;
}
}
答案 0 :(得分:1)
可能你需要定义你的字符来源。在这里,我将它传递给Scanner的构造函数。
public class Scanner {
protected char currentChar;
protected StringBuffer currentSpelling;
protected byte currentKind;
private Reader inputStreamReader;
public Scanner(InputStream inputStream) {
reader = new InputStreamReader(inputStream);
}
private void take (char expectedChar){
if (currentChar == expectedChar){
currentSpelling.append(currentChar);
currentChar = (char) inputStreamReader.read();
}
}
// Note. Probably you need to get the currentChar before append it.
// So the order of operations should be inverted
private void takeIt(){
currentSpelling.append(currentChar);
currentChar = (char) inputStreamReader.read();
}
}
要创建它,您可以执行类似的操作(或者您可以将其替换为从另一个流中读取数据,例如文件):
Scanner myScanner = new Scanner(System.in);
注意:我将类的名称从scanner
更改为Scanner
,因为将类设为大写是一种很好的做法。