使用 Scanner 讀取系統輸入

Scanner scanner = new Scanner(System.in); //Scanner obj to read System input
String inputTaken = new String();
while (true) {
    String input = scanner.nextLine(); // reading one line of input
    if (input.matches("\\s+"))         // if it matches spaces/tabs, stop reading
        break;
    inputTaken += input + " ";
}
System.out.println(inputTaken);

初始化掃描器物件以從鍵盤讀取輸入。因此,對於來自 keyboar 的以下輸入,它將產生輸出為 Reading from keyboard

Reading
from
keyboard
  //space