Results 1 to 13 of 13
- 12-05-2008, 04:33 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
xception in thread "main" java.util.NoSuchElementException: No line found
this is the exception the code is throwing when it runs:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Payroll3.inputData(Payroll3.java:229)
at Payroll3.main(Payroll3.java:24)
This is the InputFile thats being used
John Smith
9.45 40 15
Jane Doe
12.50 45 15
Harry Morgan
20.00 40 20
Carmen Martinez
25.00 35 25
Jacintha Washington
50.85 60 34
The two methods that are being used inside the loop to print out the information are
public static boolean inputData(Scanner inFile, StringBuffer employeeName, DoubleClass hourlyRate, DoubleClass hoursWorked, DoubleClass taxRate) //name and formal parameters to inputData
{//begining of inputData
if(!inFile.hasNext())
return false;
else
{
employeeName.setLength(0);
employeeName.append(inFile.nextLine());
hourlyRate.setNum(inFile.nextDouble());
hoursWorked.setNum(inFile.nextDouble());
taxRate.setNum(inFile.nextDouble());
inFile.nextLine();
return true;
}
}//end of inputData
public static void printEmployeeInfo(StringBuffer employeeName, double hourlyRate, double hoursWorked, double taxRate, double grossAmount, double netAmount) //printEmployeeInfo and its paramaters
{//start of printEmployeeInfo
final double FULL_TIME=40.0;
int count, blanks=69;
for(count=0; count<=blanks; count++)//for loop intialization, termination, counter
{//begin for loop
if(count==0)
System.out.printf("%-19s",employeeName);
if(count>19 && count<22)
System.out.print(" ");
if(count==22)
System.out.printf("%8.2f", hourlyRate); //right justification of double parameter
if (count>29 && count<32) //spaces
System.out.print(" ");
if(count==32) //right justification of double parameter
System.out.printf("%8.2f", hoursWorked);
if (count>39 && count<42) //spaces
System.out.print(" ");
if(count==42) //right justification of double parameter
System.out.printf("%8.2f", taxRate);
if (count>49 && count<52) //spaces
System.out.print(" ");
if(count==52) //right justification of double parameter
System.out.printf("%8.2f", grossAmount);
if(count>59 && count<62) //spaces
System.out.print(" ");
if(count==62) //right justification of double parameter
System.out.printf("%8.2f", netAmount);
if (count==blanks && hoursWorked>FULL_TIME)
System.out.print(" OT");
if(count==blanks) //new line
System.out.println();
}//end for loop
}//end of printEmployeeInfo
Now what I can see that it is doing, with the input file is its skipping over the second name and information and going to the third, and going by that theory it should print up the last name and set of information but its not.
this is a program thats a project for my class so its using the pieces of Java that are instructed and the print method works, it was written for the part before this particular program its the Boolean that's not working (I think.)
Here's the code for the program itself
public static void main (String []args) throws FileNotFoundException//main
{ //start of Main arguments
Scanner inFile = new Scanner (new FileReader("payrolldat.txt")); //what will change depending on the input format
StringBuffer employeeName = new StringBuffer(); //variables declared for use of printEmployeeInfo method in 7.5 before payrollprocessing code
double grossAmount=0.0, netAmount=0.0;
DoubleClass hourlyRate=new DoubleClass();
DoubleClass hoursWorked=new DoubleClass();
DoubleClass taxRate=new DoubleClass();
boolean moreData=true;
instructions(); //method call to instructions
reportTitle();//method call to reportTitle
while (moreData) //while condition
{//enter while loop
moreData=inputData(inFile, employeeName, hourlyRate, hoursWorked, taxRate);
printEmployeeInfo(employeeName, hourlyRate.getNum(), hoursWorked.getNum(), taxRate.getNum(),grossAmount, netAmount); //call to printEmployeeInfo
moreData=inputData(inFile, employeeName, hourlyRate, hoursWorked, taxRate);
}//end while
} //end main
-
This error usually happens if you call Scanner#next() without checking hasNext first. Remember you should check this or the equivalent Scanner#hasNextXXX() before every call to Scanner#nextXXX(). You're not doing this. You only check hasNextXXX() at the beginning of the loop.
- 12-05-2008, 04:43 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
it has an if statement in it to check if it hasNext before it gets the information its looking to make sure that there's a string set there o.O and it does it...twice...
- 12-05-2008, 05:02 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
ok So I put the if(inFile.hasNext()) before each statement and it now doesn't have the error but it still picks up only three of the people, it skips one between each...
-
your while loop needs work.
This logic is faulty. Can you see why it's skipping lines here (with scrutiny it should be obvious)? You can think of a better while loop, right? give it a try.Java Code:while (moreData) { moreData = getSomeData(); doSomethingWithData(); moreData = getSomeData(); // and just ignore it, don't work on it. }
Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
- 12-05-2008, 05:11 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
lol, that was the logic given by the teacher for how to write the loop. That is why it was used. But...I will see.
- 12-05-2008, 05:15 AM #7
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
AHhh now it works.
You have been an immense help thank you very very much.
- 12-05-2008, 05:15 AM #8
Member
- Join Date
- Jul 2008
- Posts
- 68
- Rep Power
- 0
my guess without debugging it is that the problem is you are calling your inputData method a second time inside that loop. this increments the scanner with inFile.nextLine() but never prints anything and then you increment again at the start of the next loop.
Edit: Fubarable beat me to it.
-
that's what I'm trying to get him to see. There are more problems with this code, but that's the main one.my guess without debugging...
-
To the original poster, let me know if you have this fixed. You have more problems and I'm going to bed soon.
- 12-05-2008, 05:31 AM #11
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
I just said it worked O.O;
Sorry if I didn't make it clear, its solved, and thank you for the point out on the loop. that was a teacher mistake I'll tell her about it tomorrow when I turn it in
-
You might want to use two Scanner objects here, one for your "inFile" and the other a temporary Scanner object created in the inputData method. Again, inFile should be checked for hasNextLine() before any line is read into it. The first line will be used to get the name, the second line should be read in its entirety into a String. This String can then be parsed by the second temporary or short-lived Scanner object, or you could do a String.split(" "); using a space, " ", as the delimiter then parse each item in the array using Double.parseDouble() method.
If you use a Scanner object for the second line, don't forget to close it when you're done with it. Again, you should check it with hasNextDouble() before trying to extract a double.
Good night and good luck.
- 12-05-2008, 05:37 AM #13
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
I believe sense it works and it does what its supposed to that that part is finished, This is all due by like midnight tomorrow and beyond the last two pieces of this lab in paritcular I have two others to do, so sense this paritculer one does what its supposed to do, gets all the values etc. I'm going to move on to finish it. This break when I have time to play with it, I'll try that though.
Thanks.
Similar Threads
-
Exception in thread "main" java.util.NoSuchElementException
By vileoxidation in forum New To JavaReplies: 5Last Post: 09-17-2008, 07:29 AM -
Exception in thread "main" java.util.NoSuchElementException
By ragav in forum New To JavaReplies: 4Last Post: 06-08-2008, 02:19 PM -
[SOLVED] Exception in thread "main" java.util.NoSuchElementException
By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 01:43 PM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks