Reputation: 21
I don't know how to write the "next source character" code to find the next character of the string.
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;
}
}
Upvotes: 2
Views: 90
Reputation: 26946
Probably you need to define your source of chars. Here I passed it to the constructor of 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();
}
}
To create it you can do something like (or you can replace it to read data from another stream, for example a file):
Scanner myScanner = new Scanner(System.in);
Note: I changed the name of class from scanner
to Scanner
because it is a good practice to have a class Capitalized.
Upvotes: 1