Array code problem Please help fairly easy
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;
}
}
}
Re: Array code problem Please help fairly easy
If you check the API docs for Scanner.nextLine(), you'll see that when there is no line found, it doesn't return null - it throws an exception. You should use Scanner.hasNextLine() before nextLine() to check that a line is present. The API docs should be your first reference for problems with unfamiliar classes and methods.
Incidentally, StringTokenizer is no longer recommended for use (although it hasn't yet been deprecated). String.split(..) or Scanner are the recommended alternatives.
Re: Array code problem Please help fairly easy
When reading a file with the Scanner class I tend to use
Code :
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
}
http://www.javaprogrammingforums.com...ner-class.html