Results 1 to 2 of 2
- 11-03-2012, 12:07 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 5
- Rep Power
- 0
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
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!
- 11-03-2012, 12:38 AM #2
Member
- Join Date
- Oct 2012
- Posts
- 5
- Rep Power
- 0
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.
Similar Threads
-
Exception in thread "main" java.util.NoSuchElementException Help Please
By ZeoVioles in forum New To JavaReplies: 11Last Post: 09-04-2012, 05:33 AM -
Need help with 'Exception in thread "main" java.util.NoSuchElementException'
By Wicked in forum New To JavaReplies: 11Last Post: 02-11-2011, 01:11 AM -
Exception in thread "main" java.util.NoSuchElementException
By vileoxidation in forum New To JavaReplies: 5Last Post: 09-17-2008, 08:29 AM -
Exception in thread "main" java.util.NoSuchElementException
By ragav in forum New To JavaReplies: 4Last Post: 06-08-2008, 03:19 PM -
[SOLVED] Exception in thread "main" java.util.NoSuchElementException
By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 02:43 PM
Bookmarks