Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Array code problem Please help fairly easy

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Thanked 0 Times in 0 Posts

    Default 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;
    if (customerIndex == -1) {

    // Customer not found, there is a new customer
    CustomerHistory order = new CustomerHistory(customerID,


    } else {

    // Customer found, update his total
    CustomerHistory order = orders.get(customerIndex);
    + totalOfOrder);


    // 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);
    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.");

  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Faversham, Kent, UK
    My Mood
    Thanked 58 Times in 54 Posts

    Default 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.
    Last edited by dlorde; April 28th, 2011 at 09:16 AM.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    United Kingdom
    My Mood
    Thanked 294 Times in 227 Posts
    Blog Entries

    Default Re: Array code problem Please help fairly easy

    When reading a file with the Scanner class I tend to use

                Scanner scanner = new Scanner(file); 
                while (scanner.hasNextLine()) {

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Little easy to fix problem
    By adammint7 in forum Java Theory & Questions
    Replies: 9
    Last Post: April 14th, 2011, 10:07 AM
  2. Array Code Help
    By whattheeff in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 21st, 2011, 04:44 PM
  3. Easy array question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 09:44 PM
  4. Need help with array code
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2010, 10:54 PM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM