考虑以下针对练习问题的代码,规范在哪里粗略地说:“在调用 readTokens() 时,等待用户按回车键,然后继续执行。”
我不完全理解为什么我的代码有效,为什么程序在显示“输入数据:”后暂停并等待回车?这种行为在哪里定义,如何改变?
import java.io.*;
import java.util.LinkedList;
public class StreamTokeTwo {
private int ttype = 0;
StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
private void setupTokenizer() {
tokenizer.resetSyntax();
tokenizer.wordChars(33, 126);
tokenizer.whitespaceChars(0, 32);
tokenizer.whitespaceChars(44,44);
tokenizer.whitespaceChars(47,47);
tokenizer.eolIsSignificant(true);
}
private String[] readTokens() {
LinkedList<String> list = new LinkedList<String>();
String[] array = null;
do {
try {
ttype = tokenizer.nextToken();
} catch (Exception e) {
System.exit(1);
}
if (ttype == StreamTokenizer.TT_WORD) {
list.add(tokenizer.sval);
}
} while (ttype != StreamTokenizer.TT_EOL);
array = list.toArray(new String[0]);
return array;
}
public static void main(String[] args) {
StreamTokeTwo streamToke = new StreamTokeTwo();
streamToke.setupTokenizer();
for (int i = 0; i < 5; ++i) {
System.out.print("Enter data: ");
String[] array = streamToke.readTokens();
for (String str : array) {
System.out.print(str + " ");
}
System.out.println();
}
}
}