簡體   English   中英

Java掃描器從我的文本文件讀取null嗎?

[英]Java scanner reading null from my text file?

我正在編寫一些代碼來讀取書名的輸入文件,並將讀取的行放入數組中並嘗試打印出該數組。 但是,當我嘗試打印出數組時,它只會為每個讀取行返回“ null”。 我不確定自己在做什么錯或我的代碼在做什么。 有什么建議么? 謝謝!

碼:

    import java.io.*;
import java.util.*;
public class LibraryInputandOutputs {
    public static void main(String args[]) throws IOException{
        int lineCount = 0;
        File inputFile = new File("bookTitles.inp.txt");
        Scanner reader = new Scanner(inputFile);
        while(reader.hasNextLine()) {
            reader.nextLine();
            lineCount++;
        }

        String[] bookArray = new String[lineCount];
        while (reader.hasNextLine()) {
            for (int i = 0; i < lineCount; i++) {
                bookArray[i] = reader.next();
             }
        }
        for (int k = 0; k < lineCount; k++) {
            System.out.println(bookArray[k]);
        }
        reader.close();
        inputFile.close();

    }
}

我正在閱讀的文本文件有20種書名,它們都在不同的行上。 我在終端上的輸出是20行的null。

讓我們分解一下:

  1. 這將讀取輸入文件的每一行,對每一行進行計數,然后將其丟棄:

     while(reader.hasNextLine()) { reader.nextLine(); lineCount++; } 

    您現在位於文件末尾。

  2. 分配足夠大的字符串數組。

     String[] bookArray = new String[lineCount]; 
  3. 嘗試閱讀更多行。 該循環將立即終止,因為reader.hasNextLine()將返回false 已經在文件末尾了。

    因此,將不會執行分配給bookArray[i]的語句。

     while (reader.hasNextLine()) { for (int i = 0; i < lineCount; i++) { bookArray[i] = reader.next(); } } 
  4. 由於bookArray[i] = ...從未在上面執行過,因此所有數組元素仍將為null

     for (int k = 0; k < lineCount; k++) { System.out.println(bookArray[k]); } 

一種解決方案是打開並讀取文件兩次。

另一個解決方案是將文件“重置”回開始。 (有點復雜。)

另一種解決方案是使用List而不是數組,這樣您就不需要兩次讀取文件。

另一個解決方案是在javadocs中搜索一種方法,該方法將以字符串數組的形式讀取文件/流的所有行。

(其中一些要求可能會因您的鍛煉要求而被排除需要進行鍛煉...)


步驟3中的嵌套循環也是錯誤的。 在while循環內不需要for循環。 您需要一個循環來“遍歷”行並增加數組索引( i )。 它們都不需要由loop語句本身完成。 您可以在循環主體中執行一個或另一個(或同時執行兩個)。

Stephen C已經指出了您的邏輯存在的主要問題。 您試圖在文件中循環兩次,但是第一次已經到達文件末尾。 不要循環兩次。 將兩個while循環“合並”為一個,在while循環中刪除for循環,並收集所有書名。 然后,您可以使用列表的大小在以后打印它們。 我的Java可能會生銹,但是在這里-

import java.io.*;
import java.util.*;
public class LibraryInputandOutputs {
    public static void main(String args[]) throws IOException {

        // int lineCount = 0; - You don't need this.

        File inputFile = new File("bookTitles.inp.txt");
        Scanner reader = new Scanner(inputFile);

        // Use an array list to collect book titles.
        List<String> bookArray = new ArrayList<>(); 

        // Loop through the file and add titles to the array list.
        while(reader.hasNextLine()) {
            bookArray.add(reader.nextLine());
            // lineCount++; - not needed
        }

        // Not needed -
        // while (reader.hasNextLine()) {
        //     for (int i = 0; i < lineCount; i++) {
        //         bookArray[i] = reader.next();
        //      }
        // }

        // Use the size method of the array list class to get the length of the list 
        // and use it for looping.
        for (int k = 0; k < bookArray.size(); k++) {  
            System.out.println(bookArray[k]);
        }

        reader.close();
        inputFile.close();

    }
}

我同意StephenC。特別是,使用List通常比數組更好,因為它更靈活。 如果需要數組,則在列表填充后始終可以使用toArray()。

您的書名是否分開? 如果是這樣,您可能不需要Scanner類,可以使用BufferedReader或LineNumberReader之類的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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