Error: NoSuchElementException when trying to read values from file to array

Error: NoSuchElementException when trying to read values from file to array

好的。所以这是本学期的最后一个 HW 作业,我遗漏了程序的很大一部分,因为这是我似乎唯一无法解决的问题。我正在设置我的 FileInputStream 并使用 for 循环将值读入数组,就像我过去所做的那样没有问题。出于某种原因,我遇到了这个异常,但无法弄清楚。我已经查看了有关此异常的许多其他线程,但同样,似乎无法弄清楚。请暂停

这是代码,可以编译;

import java.util.*;
import java.io.*;

public class CollinDunn_1_10 {

  // Declare constants
  static final int MAX_EMPLOY = 30;
  static final String TAB = "\t";
  static final String NL = "\n";
  static final double IRA_INVEST = .08;
  static final double FEDERAL_WITH = .18;
  static final double STATE_WITH = .045;
  static final double SAVINGS = .10;

public static void main (String [] args) throws Exception {

  // I/O String references
  final String INPUT = "CollinDunn_1_10_Input.txt";
  final String OUTPUT = "CollinDunn_1_10_Output.txt";

  // Declare variables
  // One-dimensional array for storing employee names
  String [] names = new String [MAX_EMPLOY];

  // Two-dimensional array for storing employee pay data
  // [0] hours worked [1] pay rate [2] gross pay [3] net pay
  // [4] savings amount [5] IRA amount [6] taxs withheld 
  double [][] payInfo = new double [MAX_EMPLOY][6];

  // Set up I/O
  FileInputStream inputDataFile = new FileInputStream(INPUT);
  Scanner inputFile  = new Scanner(inputDataFile);    
  FileWriter outputDataFile = new FileWriter(OUTPUT);
  PrintWriter outputFile = new PrintWriter(outputDataFile);

  // Read data from the file
  readData(inputFile, payInfo, names);

  // Test printing to see if values are stored - REMOVE 
  for (int i = 0; i < MAX_EMPLOY; i++) {
     System.out.print(names[i] + TAB + payInfo[i][0] + TAB + payInfo[i][1]);
  }

} // End main

 // Method for reading file data into the file.
 // Data is sorted as (number of hours) (pay rate) (name)
 public static void readData (Scanner inputFile, double [][] payInfo, String [] names) {
     for (int i = 0; i < MAX_EMPLOY; i++) {
        payInfo [i][0] = inputFile.nextDouble();
        payInfo [i][1] = inputFile.nextDouble();
        names [i] = inputFile.nextLine();
     } // End For
  return;
} // End readData
} // End Class

*文本文件中的字段如下: (小时)(工资)(姓名)

50.00 10.60 轻卡伦 L
52.00 10.80 费根·伯特·托德
62.00 12.24 安特里姆福雷斯特 N*

The Exception and stack trace:
Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Scanner.java:862)

at java.util.Scanner.next(Scanner.java:1485)

at java.util.Scanner.nextDouble(Scanner.java:2413)

at CollinDunn_1_10.readData(CollinDunn_1_10.java:56)

at CollinDunn_1_10.main(CollinDunn_1_10.java:42)

您永远不会在尝试检索值之前检查是否有要检索的值(查找 Scanner.hasNextDouble)。由于您的 for 循环经过 MAX_EMPLOY 次迭代,只要您的输入文件包含少于 MAX_EMPLOY 行数据,就会发生此异常。

您应该每次都检查您的表格或文件是否包含正确数量的元素或它们是否足够大。代码应如下所示:

public static void readData (Scanner inputFile, double [][] payInfo, String [] names) {
     Integer payInfoLenght = payInfo.lenght;
     Integer namesLenght = names.lenght;
     if (MAX_EMPLOY > payInfoLenght || MAX_EMPLOY > namesLenght) {
          System.out.println("Wrong size of tabels");
     } else {
          for (int i = 0; i < MAX_EMPLOY; i++) {
             if (inputFile.hasNextDouble()) {
                 payInfo [i][0] = inputFile.nextDouble();
             }
             if (inputFile.hasNextDouble()) {
                 payInfo [i][1] = inputFile.nextDouble();
             }
             if (inputFile.hasNextLine()) {
                 names [i] = inputFile.nextLine();
             }
          } 
     }
} 

并且您不需要在此方法的末尾 "return;",因为此方法没有任何内容 returns。这是"void"方法。