我有这样的事情:
InputStream in = new InputStream(fileName);
Scanner sc = new Scanner(in);
String nextWord;
String anotherWord;
while(sc.hasNext()) {
nextWord = sc.next();
...
...
}
while(sc.hasNext()) {
anotherWord = sc.next();
...
...
}
第二个循环不起作用,因为在sc.next()
读取令牌后扫描程序的缓冲区被清除。如何在不清除扫描仪缓冲区的情况下读取令牌,以便在第二个循环中使用sc
?
答案 0 :(得分:0)
您可以像这样重新初始化扫描仪。
InputStream in = new InputStream(fileName);
Scanner sc = new Scanner(in);
String nextWord;
String anotherWord;
while(sc.hasNext()) {
nextWord = sc.next();
...
...
}
sc = new Scanner(in);
while(sc.hasNext()) {
anotherWord = sc.next();
...
...
}
答案 1 :(得分:0)
nextWord = sc.next();
nextToNextWord = sc.next();
nextToNextToNext = sc.next();
anotherWord = sc.next();
anotherOneWord= sc.next()
anotherTwoWord = sc.next()
这里不需要循环读取,除非进入数组或Collection
。