Okay. Das ist also der letzte HW-Auftrag des Semesters, ich verlasse einen großen Teil des Programms, weil das das Einzige ist, was ich nicht reparieren kann. Ich richte meinen FileInputStream ein und benutze eine for-Schleife, um Werte in das Array zu schreiben, wie ich es in der Vergangenheit ohne Probleme getan habe. Aus irgendeinem Grund bekomme ich diese Ausnahme und kann es nicht herausfinden. Ich habe mir viele andere Threads rund um diese Ausnahme angesehen, aber ich kann es auch nicht herausfinden. Bitte halbieren.Fehler: NoSuchElementException beim Versuch, Werte von Datei zu Array zu lesen
Hier ist der Code, es kompiliert;
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
* Die Felder auf der Textdatei sind als so: (Stunden) (gebührenpflichtig) (Name)
50,00 10,60 Licht 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)
Vielen Dank. Das hat alles klarer gemacht :) – EmergentSpecter