-3
somefile.txt
有一些輸入如下,newfile.txt
爲空。爲什麼這個簡單的代碼會拋出「NoSuchElementException」?
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();
}
}
我修復了你的問題措辭和格式,盡我所能。這裏缺少的主要的其他東西是完整的例外,包括堆棧跟蹤以及它發生在哪一行。 – smarx