Results 1 to 2 of 2
Like Tree2Likes
  • 2 Post By Mattiscool

Thread: Error: "Exception in thread "main" java.util.NoSuchElementException"

  1. #1
    Mattiscool is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Error: "Exception in thread "main" java.util.NoSuchElementException"

    Hi everyone, I'm currently working on a project to read data from another .txt file and manipulate it.
    Here's my code:
    Java Code:
    /*
    Author: Matthew Morvant
    School: Falmouth High School
    Project 8.5
    The project selected for this is Project 3-5 on page 103.
    Essentially, this involves changing the input data stream to include
    five days of information in order to generate the week's pay.
    The organization of the data file is worker's first name, last name, hourly
    wage and then 5 sets of two numbers representing each day of the week.
    The first number in the pair represents regular hours for that day while
    the second number represents overtime hours for that day.
    */
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import java.lang.*;
    import java.awt.*;
    class Project3Point5{
      // program variables to hold data
      private static String line, firstName, middleInitial, lastName;
      private static double wage, regHoursMonday, overtimeMonday,regHoursTuesday, overtimeTuesday;
      private static double regHoursWednesday, overtimeWednesday, regHoursThursday;
      private static double overtimeThursday, regHoursFriday, overtimeFriday;
      private static double totalRegHours, totalOvertime, totalPay;
    
      // variables necessary for file input
      private static FileInputStream inFile;
      private static InputStreamReader inReader;
      private static BufferedReader reader;
    
      //  StringTokenizer variable used to separate line into different data elements
      private static StringTokenizer strTkn;
    
      // throws IOException will have the operating system handle any problems
      // that may occur while attempting to acquired data from the file.
      
      public static void main (String args[]) throws IOException
      {
    
        initFile();  //Prep the file for input
        getData(); //extract data from file
        calcTotal(); //Calculate total hours and pay
        printResults(); //Print out the results
        
        // closing the data file
        inFile.close();
      }
      
     
     // preparing the file for input
     
      public static void initFile() throws IOException
      {
        inFile = new FileInputStream ("c:\\!!VHSAPCSData\\VHSP35data1_1.txt");
        inReader = new InputStreamReader(inFile);
        reader = new BufferedReader(inReader);
      }
      
      
      //data acquisition method
      
      public static void getData() throws IOException
      {
        line = reader.readLine();   //acquiring the data line as a string
        System.out.println ("data line = " + line ); // view data line as one string
        System.out.println();
        
        strTkn = new StringTokenizer(line);  // attaching the string tokenizer to the line
        
        // placing the first word in the dataline into a string variable    
        firstName = strTkn.nextToken();
        middleInitial = strTkn.nextToken();
        lastName = strTkn.nextToken();
         
        // extracting the parts of the line into separate int and double variables
        wage = Double.parseDouble(strTkn.nextToken());
        regHoursMonday = Double.parseDouble(strTkn.nextToken());
        overtimeMonday = Double.parseDouble(strTkn.nextToken());
        regHoursTuesday = Double.parseDouble(strTkn.nextToken());
        overtimeTuesday = Double.parseDouble(strTkn.nextToken());
        regHoursWednesday = Double.parseDouble(strTkn.nextToken());
        overtimeWednesday = Double.parseDouble(strTkn.nextToken());
        regHoursThursday = Double.parseDouble(strTkn.nextToken());
        overtimeThursday = Double.parseDouble(strTkn.nextToken());
        regHoursFriday = Double.parseDouble(strTkn.nextToken());
        overtimeFriday = Double.parseDouble(strTkn.nextToken());
      }  
        
       // processing the double totals   
      public static void calcTotal()
      {
        totalRegHours = (regHoursMonday + regHoursTuesday + regHoursWednesday + regHoursThursday + regHoursFriday);
        totalOvertime = (overtimeMonday + overtimeTuesday + overtimeWednesday + overtimeThursday + overtimeFriday);
        totalPay = ((totalRegHours * wage) + (totalOvertime * 1.5));
      }  
    
        // output results
    
      public static void printResults()
      {
        System.out.println(firstName + " " + middleInitial + ". " + lastName + " makes " + wage + "$ an hour.");
        System.out.println("On Monday, " + firstName + " worked " + regHoursMonday + " regular hours, and " + overtimeMonday + "overtime hours.");
        System.out.println("On Tuesday, Mr. " + lastName + " worked " + regHoursTuesday + " regular hours, and " + overtimeTuesday + "overtime hours.");
        System.out.println("On Wednesday, he worked " + regHoursWednesday + " regular hours, and " + overtimeWednesday + "overtime hours.");
        System.out.println("On Thursday, he worked " + regHoursThursday + " regular hours, and " + overtimeThursday + "overtime hours.");
        System.out.println("On Friday, he worked " + regHoursFriday + " regular hours, and " + overtimeFriday + "overtime hours.");
        System.out.println("In total, this is " + totalRegHours + " regular hours, and " + totalOvertime + " overtime hours.");
        System.out.println("With his wage of " + wage + "$/Hr, " + firstName + " " + lastName + " made " + totalPay + "$ this week");
        
      }    
    } // end of class
    Here's the Error
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:349)
    at Project3Point5.getData(Project3Point5.java:86)
    at Project3Point5.main(Project3Point5.java:41)

    Here's the .txt file that I'm reading:

    Java Code:
    Kermit D.Frogge  17.25 5.25 0.0 8.0 1.5 8.0 2.25 7.75 0.0 8.0 2.0

    If you could help me identify this error, that'd be amazing. I've spent about two hours on this one thing, and it's very frustrating
    Thanks everyone!

  2. #2
    Mattiscool is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: Error: "Exception in thread "main" java.util.NoSuchElementException"

    Found the error: In the .txt file that I'm reading from, there's a typo.
    "Kermit D.Frogge" This would be classified as 2 'words,' because I don't have '.' as a delimiter. I changed this part of the text to "Kermit D. Frogge" and everything worked fine.
    Fubarable and SJF like this.

Similar Threads

  1. Replies: 11
    Last Post: 09-04-2012, 04:33 AM
  2. Replies: 11
    Last Post: 02-11-2011, 12:11 AM
  3. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 07:29 AM
  4. Replies: 4
    Last Post: 06-08-2008, 02:19 PM
  5. Replies: 5
    Last Post: 05-14-2008, 01:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •