大家好,我想读取一个我之前已经做过很多次的文件,但是无论.NET中存在多行代码,它都会一直输出“ null”。



    public static void main(String[] args) {
        // TODO code application logic here
    Scanner kbd = new Scanner(System.in);
    String[] item = new String[25];
   Scanner fileInput;
   File inFile = new File("dictionary.txt");
    try {
    fileInput = new Scanner(inFile);
    int newItem = 0;
    while (fileInput.hasNext())
    {
      item[newItem++] = fileInput.nextLine();
      System.out.println(item[newItem]);
    }
    }
    catch(FileNotFoundException e){System.out.println(e); }





txt文件。请帮忙。

最佳答案

您增加newItem,然后打印item[newItem]。它始终返回null,因为尚未在item中为新索引编写任何内容。

尝试:

while (fileInput.hasNext()) {
    item[newItem] = fileInput.nextLine();
    System.out.println(item[newItem]);
    newItem++;
}

09-26 21:33
查看更多