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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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

EJP
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?
It means that when you called nextToken() there weren't any more tokens. In other words the input line didn't contain as many tokens as you expected.
807601
Wouldn't the code just read in what was in the .txt file and when it hit while (line != null) then print the output? I do not see where the limit on reading in the tokens is.
807601
As you are not defining any delimiter for tokenizer, by default it will take space as a delimiter. In the program you are expecting always 3 tokens in a line. Without checking any number of tokens in each line.
May be in one of the line the space was missing between the tokens.
Check the input file for the correctness.
807601
To be sure I also would suggest using hasMoreTokens() to iterate over all the tokens.
1 - 4
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,221 views