Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Error in StringTokenizer

807601Dec 11 2007 — edited Dec 11 2007
I am trying to run this code that I got from a textbook. The only thing that I changed was the name of the file that was to be read in. It was "inventory.dat" and I changed it to "clients.txt".

The code compiled fine but when I went to run it I got the following error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at CheckInventory.man(CheckInventory.java:34)

What does this error mean? And why didn't it work? I took it right out of my textbook. Any insight would be appreciated.

//********************************************************************
//  CheckInventory.java       Author: Lewis/Loftus
//
//  Demonstrates the use of a character file input stream.
//********************************************************************

import java.io.*;
import java.util.StringTokenizer;

public class CheckInventory
{
   //-----------------------------------------------------------------
   //  Reads data about a store inventory from an input file,
   //  creating an array of InventoryItem objects, then prints them.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      final int MAX = 100;
      InventoryItem[] items = new InventoryItem[MAX];
      StringTokenizer tokenizer;
      String line, name, file = "clients.txt";
      int units, count = 0;
      float price;

      try
      {
         FileReader fr = new FileReader ("clients.txt");
         BufferedReader inFile = new BufferedReader (fr);

         line = inFile.readLine();
         while (line != null)
         {
            tokenizer = new StringTokenizer (line);
            name = tokenizer.nextToken();
            try
            {
               units = Integer.parseInt (tokenizer.nextToken());
               price = Float.parseFloat (tokenizer.nextToken());
               items[count++] = new InventoryItem (name, units, price);
            }
            catch (NumberFormatException exception)
            {
               System.out.println ("Error in input. Line ignored:");
               System.out.println (line);
            }
            line = inFile.readLine();
         }

         inFile.close();

         for (int scan = 0; scan < count; scan++)
            System.out.println (items[scan]);
      }
      catch (FileNotFoundException exception)
      {
         System.out.println ("The file " + file + " was not found.");
      }
      catch (IOException exception)
      {
         System.out.println (exception);
      }
   }
}
//********************************************************************
//  InventoryItem.java       Author: Lewis/Loftus
//
//  Represents an item in the inventory.
//********************************************************************

import java.text.DecimalFormat;

public class InventoryItem
{
   private String name;
   private int units;    // number of available units of this item
   private float price;  // price per unit of this item
   private DecimalFormat fmt;

   //-----------------------------------------------------------------
   //  Sets up this item with the specified information.
   //-----------------------------------------------------------------
   public InventoryItem (String itemName, int numUnits, float cost)
   {
      name = itemName;
      units = numUnits;
      price = cost;
      fmt = new DecimalFormat ("0.##");
   }

   //-----------------------------------------------------------------
   //  Returns information about this item as a string.
   //-----------------------------------------------------------------
   public String toString()
   {
      return name + ":\t" + units + " at " + price + " = " +
             fmt.format ((units * price));
   }
}
clients.txt
Widget 14 3.35
spoke 132 0.32
wrap 58 1.92
thing 28 4.17
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 8 2008
Added on Dec 11 2007
4 comments
1,320 views