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();
添加回答
举报