這可能是最簡單的事情之一,但我沒有看到我做錯了什麼。爲什麼nextLine()返回一個空字符串?
我的輸入包含一個帶有數字的第一行(要讀取的行數),一堆帶有數據的行以及只有\ n的最後一行。我應該處理這個輸入,並在最後一行之後,做一些工作。
我有這個輸入:
5
test1
test2
test3
test4
test5
/*this is a \n*/
以及讀取輸入我有這樣的代碼。
int numberRegisters;
String line;
Scanner readInput = new Scanner(System.in);
numberRegisters = readInput.nextInt();
while (!(line = readInput.nextLine()).isEmpty()) {
System.out.println(line + "<");
}
我的問題是爲什麼我不打印任何東西?程序讀取第一行,然後什麼都不做。
btw,不是第一個數字是測試的數量? – RiaD
嘗試用行!= null替換!line.isEmpty()? – user2147970
是的,它是測試次數 – Favolas