2 回答

TA貢獻1869條經驗 獲得超4個贊
如果用戶輸入:
7號車
閱讀該行所需要做的就是:
String words[] = scan.nextLine().split(" ");
現在你有一個包含該行單詞的數組。例如:words[0]
將包含汽車,words[1]
將包含 7 等。

TA貢獻1851條經驗 獲得超5個贊
您也可以一次閱讀它們并更改分隔符。這里有幾個例子。
String text = "The quick brown fox jumped over the lazy dog";
Scanner scan = new Scanner(text);
while (scan.hasNext()) {
System.out.print(scan.next() + " ");
}
System.out.println();
而這個改變了單詞之間的分隔符。您可以指定一個regular expression pattern或一個簡單的String.
text = "The:quick,+-brown::::fox:.:jumped++over,,,,the,+,+lazy---dog";
scan = new Scanner(text);
// regular expression delimiter. Any combo of one or more of the chars.
scan.useDelimiter("[:,.;+-]+");
while (scan.hasNext()) {
System.out.print(scan.next() + " ");
}
System.out.println();
添加回答
舉報