我用這個代碼: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