2016-04-27 3 views
2

文字列の次の文字を見つけるための "次のソース文字"コードの記述方法がわかりません。Javaのパーサのスキャナで次の文字を取得する方法

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

forループを使用します。 * String * .charAt() - メソッドのインデックスを使用してください。 – Seth

答えて

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に変更する必要があります。これは、クラスをCapitalizedにすることをお勧めします。

+0

そして 'currentChar'の最初の値を読み取るコンストラクタです。そして、ファイルの終わりのテスト。ニース。 –

関連する問題