概要
Javaで「スキャナの入力ストリームにまだ値が残っているか確認する」の動作を確認してみました。以下のページを参考にしました。
実装
以下のファイルを作成しました。
JSample6_5.java
import java.util.Scanner;
import java.util.regex.Pattern;
class JSample6_5{
public static void main(String[] args){
String regex = "[A-Z0-9]{2}";
var p = Pattern.compile(regex);
Scanner scanner = new Scanner("AB 7B DEB A5");
while (scanner.hasNext(p)){
System.out.println(scanner.next());
}
}
}
以下のコマンドを実行しました。
$ javac JSample6_5.java
$ java JSample6_5
AB
7B
まとめ
何かの役に立てばと。