DustCityDesign
DustCityDesign

Reputation: 5

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

Okay. So this is the last HW assignment of the semester, I am leaving out a big part of the program because this is the only thing I can't seem to fix. I am setting up my FileInputStream and using a for loop to read values into the array as I have done in the past without problems. For some reason I am getting this exception and can't figure it out. I have looked at plenty of other threads around this exception but likewise, cant seem to figure it out. Please halp.

Here is the code, it compiles;

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

*The fields on the text file are as so: (hours) (pay) (name)

50.00 10.60 Light Karen L
52.00 10.80 Fagan Bert Todd
62.00 12.24 Antrim Forrest 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)

Upvotes: 0

Views: 281

Answers (2)

lukamate
lukamate

Reputation: 71

You should everytime check if your tables or your file contains right amounts of elements or if they are big enought. Code should look like:

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();
             }
          } 
     }
} 

And you don't need "return;" at the end of this method becouse this method nothing returns. It's "void" method.

Upvotes: 0

rem
rem

Reputation: 1388

You're never checking whether there is a value to retrieve before trying to retrieve it (look up Scanner.hasNextDouble). Since your for-loop goes through MAX_EMPLOY iterations, this exception will occur whenever your input file contains less than MAX_EMPLOY lines of data.

Upvotes: 1

Related Questions