ScannerではちょっとやりづらいところがありますのでReaderを使う方がよいと思います。Javaのbyte/charを読むためのクラス群にあるreadyを利用するとお望みのことに近いことができそうです。
java
1import java.io.BufferedReader;
2import java.io.IOException;
3import java.io.InputStreamReader;
4import java.nio.charset.StandardCharsets;
5
6class SkipSample {
7 public static void main(String[] args) throws Exception {
8 try (var isr = new InputStreamReader(System.in, StandardCharsets.UTF_8);
9 var br = new BufferedReader(isr)) {
10 for (;;) {
11 skipPendingInput(br);
12 System.out.println("enter:");
13 String line = br.readLine();
14 if (line == null)
15 break;
16 System.out.println("input is: " + line);
17 System.out.flush();
18 //
19 Thread.sleep(3000);
20 System.out.println("processing completed");
21 }
22 }
23 }
24
25 static void skipPendingInput(BufferedReader br) throws IOException {
26 int skipCount = 0;
27 while (br.ready()) {
28 int c = br.read();
29 if (c == -1) break;
30 skipCount++;
31 }
32 if (skipCount > 0) {
33 System.out.println(skipCount + " character(s) skipped");
34 }
35 }
36}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/07 08:14
2018/05/07 08:18
2018/05/07 08:22