System.out.print("Enter some stuff:");
while (input.hasNext()){
System.out.print(input.next()+ " ");
}
每当此项运行时,它会要求用户输入,然后将其全部打印出来。但是,我想要的是一个打印出扫描仪所有标记的循环。然后,它意识到没有更多的令牌,并且循环退出。
答案 0 :(得分:2)
好吧,你想把这些令牌读成ArrayList
,就像这样:
List<String> store = new ArrayList<String>();
// read them all in and add them to our list
while (input.hasNext())
store.add(input.next());
// now print them all out
for (String s: store)
System.out.print(s+ " ");
这样做是为了全部阅读,并将它们放入ArrayList
;然后,当没有什么可读的时候,读取循环退出。之后,它将它们全部打印出来。我想这就是你的想法。如果你想在添加它们时打印它们,那么你可以
List<String> store = new ArrayList<String>();
// read them all in and add them to our list
while (input.hasNext()) {
String s = input.next();
store.add(s);
System.out.print(s+ " ");
}
for (String s: store) {
// do whatever you like with them
}
答案 1 :(得分:0)
不要复制,尝试了解伙伴:
import java.io.*;
import java.util.StringTokenizer;
public class PrintStuff {
public static void main (String[] args) {
System.out.print("Enter some shit seperated by space:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str="";
try {
str = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read bitch!");
}
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
}
}