[英]Why would this simple code throw "NoSuchElementException"?
somefile.txt
有如下輸入, newfile.txt
為空。
China
1330044605
India
1147995898
United States
303824646
這兩個文件都在我的桌面上。
public class NextMethod {
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File("/home/cyn/Desktop/somefile.txt");
Scanner in = new Scanner(inputFile);
PrintWriter writer = new PrintWriter("/home/cyn/Desktop/newfile.txt");
while (in.hasNextLine()) {
String coName = in.nextLine();
int peopCo = in.nextInt();
in.nextLine();
writer.println(coName);
writer.println(peopCo);
}
in.close();
writer.close();
}
}
我能夠通過在someFile.txt
的末尾添加一個空行來復制您的問題。
這是與javadoc 中記錄的行為一致的行為。
拋出: NoSuchElementException - 如果沒有找到行
檢查以確保您的輸入文檔中沒有任何意外的空白。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.