Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

Java Exception in thread "main" java.util.NoSuchElement, Please help me

views
     
TSqieroxer89
post Oct 18 2011, 05:06 PM, updated 15y ago

Getting Started
**
Junior Member
70 posts

Joined: May 2008
From: KUALA KANGSAR


CODE
import java.io.*;
import java.util.*;

public class ShinyGold
{
   static BufferedReader keyboard = new
         BufferedReader(new InputStreamReader(System.in));

   public static void main(String[] args)
   {

      CustomerList custList = new CustomerList();
      JewelList jewelList = new JewelList();
      int choice, id;
      String title;

      try
      {
          BufferedReader infile =
                  new BufferedReader(new FileReader("c:\\jewelDat.txt"));

          BufferedReader infileCust =
                  new BufferedReader(new FileReader("c:\\custDat.txt"));


          createJewelList(infile, jewelList);
          createCustomerList(infileCust, custList);

 System.out.print("");

          displayMenu();
          System.out.println("");
          System.out.print("Please enter your choice: ");
          choice = Integer.parseInt(keyboard.readLine());
          System.out.println();

          while(choice != 9)
          {
              switch(choice)
              {
              case 1: jewelList.print();
                      break;

              case 2: System.out.print("Enter the title: ");
                      title = keyboard.readLine();
                      System.out.println();

                      if(jewelList.jewelSearch(title))
                      {
                          if(jewelList.isJewelAvailable(title))
                          {
                                  System.out.print("Enter the customer id: ");
                                  id = Integer.parseInt(keyboard.readLine());
                                  System.out.println();

                                  if(custList.custSearchId(id))
                                  {
                                     jewelList.jewelCheckOut(title);
                                     custList.custRentJewel(id, title);

                                      System.out.println("Enjoy your movie: "
                                              + title);
                                  }
                                  else
                                      System.out.println("Not a customer");
                          }
                          else
                              System.out.println("Currently " + title
                                         + " is out of stock.");
                      }
                      else
                          System.out.println("The store does not "
                                          + "carry" + title);
                      break;
              case 3: System.out.print("Enter the title: ");
                      title = keyboard.readLine();
                      System.out.println();
                      if(jewelList.jewelSearch(title))
                      {
                          System.out.print("Enter the customer id: ");
                          id = Integer.parseInt(keyboard.readLine());
                          System.out.println();


                          if(custList.custSearchId(id))
                          {
                              jewelList.jewelCheckIn(title);
                              custList.custReturnJewel(id, title);
                              System.out.println("Thanks for returning "
                                         + title);
                          }
                          else
                              System.out.println("Not a customer");
                      }
                      else
                          System.out.println("This jewel is not "
                                       + "from our store");
                      break;
              case 4: System.out.print("Enter the title: ");
                      title = keyboard.readLine();
                      System.out.println();
                      if(jewelList.jewelSearch(title))
                      {
                          if(jewelList.isJewelAvailable(title))
                              System.out.println(title
                                        + " is currently in stock");
                          else
                              System.out.println(title +
                                             " is not in the store");
                      }
                      else
                          System.out.println(title + " is not "
                                             + "in store.");
                  break;

              case 5: jewelList.jewelPrintProduct();
                      break;

              case 6: System.out.print("Please enter the customer's name: ");
                            title = keyboard.readLine();
                            System.out.println();
                            if(jewelList.jewelSearch(title))
                                System.out.println("Jewellery found");
                            else
                                System.out.println(title+" is not found");
                      break;

              case 7: custList.printCustList();
                      break;

              case 8: custList.rentedJewelsInfo();
                      break;
              default: System.out.println("Invalid selection");
              }//end switch

              displayMenu();
              System.out.println("");
              System.out.print("Please enter your choice: ");
              choice = Integer.parseInt(keyboard.readLine());
              System.out.println();
          }//end while
      }
      catch(FileNotFoundException fnfe)
      {
          System.out.println(fnfe.toString());
      }
      catch(IOException ioe)
      {
          System.out.println(ioe.toString());
      }
   }

   public static void createJewelList(BufferedReader infile,
                                      JewelList jewelList)
                                      throws IOException
   {
      String  Product;
      String  Type;
      String  Purity;
      String  Producer;
      String  Director;
      String  ProductionCo;
      int   InStock;

      JewelElement newJewel = new JewelElement();

      Product = infile.readLine();

          while(Product != null)
      {
          Type = infile.readLine();
          Purity = infile.readLine();
          Producer = infile.readLine();
          Director = infile.readLine();
          ProductionCo = infile.readLine();
          InStock = Integer.parseInt(infile.readLine());
          newJewel.setJewelInfo(Product,Type,Purity,Producer,
                              Director,ProductionCo,InStock);
          jewelList.insertFirst(newJewel);

          Product = infile.readLine();
      }//end while
   }//end createJewelList


   public static void createCustomerList(BufferedReader infile,
                                     CustomerList custList)
                                      throws IOException
   {
      String line;
      String  fname;
      String  lname;
      int id;

      Customer cust= new Customer();

      StringTokenizer strToken;
      line = infile.readLine();

      while(line != null)
      {
          strToken = new StringTokenizer(line);
          fname = strToken.nextToken();
          lname = strToken.nextToken();
          id = Integer.parseInt(strToken.nextToken());
          cust.setCustInfo(fname,lname,id);
          custList.insertFirst(cust);
          line = infile.readLine();
      }
   }


   public static void displayMenu()
   {
       System.out.println("");
       System.out.println("    ****************************************");
       System.out.println("    *                                      *");
       System.out.println("    *         Welcome To ShinyGold         *");
       System.out.println("    *    A Center For Your Gold Desires    *");
       System.out.println("    *                                      *");
       System.out.println("    ****************************************");
       System.out.println("");
       System.out.println("1: Show All Available Jewellery");
       System.out.println("2: To check out a jewel");
       System.out.println("3: To check in a jewel");
       System.out.println("4: To see if a particular jewel "
                        + "is in the store");
       System.out.println("5: To print the titles of all jewels");
       System.out.println("6: To check if a particular jewel"
                        + " is in the store");
       System.out.println("7: Print a list of all the customers");
       System.out.println("8: Print a list showing which customer has "
                        + "rented which jewel");
       System.out.println("9: To exit");
   }
}


There is another class file that connected. Before this I compile there is no error, suddenly I compile it again, it shows an error
"Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at ShinyGold.createCustomerList(ShinyGold.java:205)
at ShinyGold.main(ShinyGold.java:27)"

what problem i'have done? please help


extremeJ
post Oct 20 2011, 02:24 PM

Getting Started
**
Junior Member
156 posts

Joined: Feb 2006


Before you use .nextToken in StringTokenizer, be sure to check if it has next token to proceed, otherwise you may hit exception.

i.e.
CODE

if (strToken.hasMoreTokens() == true) {
 fname = strToken.nextToken();
}


 

Change to:
| Lo-Fi Version
0.0253sec    0.63    5 queries    GZIP Disabled
Time is now: 13th December 2025 - 09:35 PM