I am getting this error code when running my program and i am not sure why.
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at customertotaler.Totaler.main(Totaler.java:47)
Here is my code the line in yellow is the line the error message refers to but i cant tell why:
package customertotaler;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* @author lopezr
*/
public class Totaler {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
File inputFile = new File("CustomerData.txt");
// Check if the file exists.
if (!inputFile.exists()) {
System.out.println("The input file was not found.");
return; // abort
}
//
// Refer to Collections in the Sun Java API Documentation. Determine
// which Collection implementation would be the best choice for your
// application.
//
// In this case, I choose the ArrayList.
//
ArrayList<CustomerHistory> orders = new ArrayList<CustomerHistory>();
// Process the input file
try {
// Create the reader
Scanner reader = new Scanner(inputFile);
// Read the file
String line;
while ((line = reader.nextLine()) != null) { // Each data is a token. The class StringTokenizer make things
// easier to us...
StringTokenizer tokens = new StringTokenizer(line);
if (!tokens.hasMoreTokens()) {
break; // empty line, should be the end of file...
}
// Store the tokens in temporary variables
int customerID = Integer.parseInt(tokens.nextToken());
tokens.nextToken(); // skip the order number, we don't need it.
double totalOfOrder = Double.parseDouble(tokens.nextToken());
// Try to locate the customer
int customerIndex = -1;
for (int i = 0; i < orders.size(); i++) {
if (orders.get(i).getCustomerID() == customerID) {
customerIndex = i;
break;
}
}
if (customerIndex == -1) {
// Customer not found, there is a new customer
CustomerHistory order = new CustomerHistory(customerID,
totalOfOrder);
orders.add(order);
} else {
// Customer found, update his total
CustomerHistory order = orders.get(customerIndex);
order.setTotalOfOrder(order.getTotalOfOrder()
+ totalOfOrder);
}
}
reader.close();
// Output the total spent for each customer
System.out.println("Total Spent For Each Customer");
System.out.println("\nCustomer\t Total");
double sum = 0;
for (int i = 0; i < orders.size(); i++) {
CustomerHistory order = orders.get(i);
System.out.println(order);
sum += order.getTotalOfOrder();
}
// Additional: print the total spent for all customers
System.out.println("\nSum:\t\t" + String.format("%8.2f", sum));
} catch (IOException ioe) {
System.out.print("Failed to read the input file.");
return;
}
}
}