-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