簡體   English   中英

從Java中的文本文件填充2D數組(拋出NoSuchElementExcecption)

[英]Fill a 2D array from a text file in java (NoSuchElementExcecption thrown)

我正在嘗試從文本文件中填充2D數組(它只是pi到小數點后第900位,除了3.14之外沒有空格或其他任何內容)。現在它將打印我所在的行和列索引,以及計數器(最終將被刪除)。

import java.util.Scanner;

public class Array {

public static void main(String[] args) throws Exception {
    // instantiate input file
    java.io.File file = new java.io.File("C:\\arrayinput.txt");

    // instantiate scanner to read file
    Scanner scanner = new Scanner(file);

    // instantiate array
    String[][] array = new String[20][45];

    // instantiate counter
    int i = 1;

    // loop to read file and assign a character to each array element
    for (int rows = 0; rows < array.length; rows++) {
        for (int columns = 0; columns < array[rows].length; columns++) {
            System.out.println("Row: " + rows + ", column: " + columns + ", counter = " + i);
            i++;
            array[rows][columns] = scanner.next();

        } // end inner for loop

    } // end outer for loop
    scanner.close();
}// end main method

}

該代碼將循環兩次並顯示:

行:0,列:0,計數器= 1

行:0,列:1,計數器= 2

線程“主”中的異常java.util.NoSuchElementException

  at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at Array.main(Array.java:30) 

我不明白為什么在沒有代碼的情況下運行此異常

array[rows][columns] = scanner.next();

如我所料,它將遍歷0-19行和0-39列,並且計數器將一直迭代到900(因為文本文件pi到第900位)。 但是,當它開始將文本文件中的每個字符分配給每個元素時,我到達第0行第2列時會拋出錯誤,我也不知道為什么。 我的arrayinput.txt文件的內容:

3。

始終檢查您的掃描儀是否具有hasNext()令牌,然后考慮調用next() 試試這個:

我修復了一些錯誤,我的編譯器抱怨您沒有被捕獲的異常。 最后在finally塊(或您的嘗試資源)中關閉掃描儀。

public static void main(String[] args) {
    // instantiate input file
    File file = new java.io.File("file.txt");
    Scanner scanner = null;
    // instantiate scanner to read file
    try {
      scanner = new Scanner(file);

      // instantiate array
      String[][] array = new String[20][45];

      // instantiate counter
      int counter = 1;

      // loop to read file and assign a character to each array element
      for (int i = 0; i < array.length; i++) {
          for (int j = 0; j < array[i].length; j++) {
              System.out.println("Row: " + i + ", column: " + j + ", counter = " + counter++);
              if (scanner.hasNext()) {
                array[i][j] = scanner.next();  
              }
          } // end inner for loop

      } // end outer for loop
    } catch(FileNotFoundException e) {
        System.err.println("There is no such file");
    }finally{
        scanner.close();
    }
  }

編輯

輸出:

Row: 0, column: 0, counter = 1
Row: 0, column: 1, counter = 2
Row: 0, column: 2, counter = 3
Row: 0, column: 3, counter = 4
Row: 0, column: 4, counter = 5
Row: 0, column: 5, counter = 6
Row: 0, column: 6, counter = 7
Row: 0, column: 7, counter = 8
Row: 0, column: 8, counter = 9
Row: 0, column: 9, counter = 10
Row: 0, column: 10, counter = 11
Row: 0, column: 11, counter = 12
...

暫無
暫無

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

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