从Java中的文本文件填充2D数组(抛出NoSuchElementExcecption)

时间:2018-11-27 20:29:33

标签: java arrays for-loop java.util.scanner nosuchelementexception

我正在尝试从文本文件中填充一个2D数组(它只是pi到小数点后第900位,除了3.14之外没有空格或其他任何内容...)现在它将打印我所在的行和列索引,以及计数器(最终将被删除)。

def sum13(nums):
    sum = 0
    last = None
    for i in nums:
        if i != 13 and last != 13:
             sum += i
        last = i
    return sum

此代码将循环两次并显示:

  

行:0,列:0,计数器= 1

     

行:0,列:1,计数器= 2

     

线程“ main”中的异常java.util.NoSuchElementException

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

}

我不明白为什么在没有代码的情况下运行此异常

 at java.util.Scanner.throwFor(Unknown Source)

 at java.util.Scanner.next(Unknown Source)

 at Array.main(Array.java:30)

它将按我期望的那样遍历0-19行和0-39列,并且计数器将一直迭代到900(因为文本文件pi到第900位)。但是,当它开始将文本文件中的每个字符分配给每个元素时,我到达第0行第2列时会抛出错误,我也不知道为什么。 我的arrayinput.txt文件的内容:

  

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473

1 个答案:

答案 0 :(得分:1)

始终检查您的扫描仪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
...