2016-09-18 96 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 - 如果沒有行被發現

檢查,以確保你沒有你的輸入文檔中的任何意外的空白。