2016-09-18 97 views
-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(); 

    } 

} 
+0

我修复了你的问题措辞和格式,尽我所能。这里缺少的主要的其他东西是完整的例外,包括堆栈跟踪以及它发生在哪一行。 – smarx

回答

0

我能够通过在someFile.txt的末尾添加空白行来复制您的问题。

这与javadoc中记录的内容一致。

抛出:NoSuchElementException - 如果没有行被发现

检查,以确保你没有你的输入文档中的任何意外的空白。

相关问题