我使用这段代码:
public String processFile(Scanner scanner) {
String result = "";
SumProcessor a = new SumProcessor();
AverageProcessor b = new AverageProcessor();
String line = null;
while (scanner.hasNext()) {
if (scanner.hasNext("avg") == true) {
c = scanner.next("avg");
while(scanner.hasNextInt()){
int j = scanner.nextInt();
a.processNumber(j);
}
System.out.println("Exit a");
result += a.getResult();
a.reset();
}
if (scanner.hasNext("sum") == true) {
c = scanner.next("sum");
while(scanner.hasNextInt()){
int j = scanner.nextInt();
b.processNumber(j);
}
System.out.println("Exit b");
result += b.getResult();
b.reset();
}
}
return result;
}
当我按下回车或发送空行时,我需要在循环(hasNexInt())时结束。
我尝试使用 String == null 等的一些方法,但 Java 只是 IGNORE 空行
输出
run:
avg
1
2
3
4
sum
Exit a
1
2
3
4
但是我需要 :
run:
avg
1
2
3
4
Exit a
sum
1
2
3
4