簡體   English   中英

為什么這個簡單的代碼會拋出“NoSuchElementException”?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM