我用这个代码:scanNextInt()忽略空行
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;
}
,我需要结束while循环(hasNexInt()),当我按下输入或发送空行。
我尝试用一些方法与字符串== NULL等,但Java的忽略空行
输出
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
已经尝试此 – JohnDow
固定码和控制台输出 – JohnDow