File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes problem with StringBuffer and Double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with StringBuffer and Double" Watch "problem with StringBuffer and Double" New topic

problem with StringBuffer and Double

Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
here is the payroll.dat file I'm trying to read:

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

here is the code to read it and print the results:

As it is there it reads and prints the first employee and data but then I get an input mismatch exception trying to read the second employee, while this code does fine for all but the last (fifth) employee:

How do I get the fifth employee to print out. BTW, gross and net Amounts are previously set to 0.0 for this exercise.

Update: I found that if I use the latter code with an extra " " (space) after the last token in the file, the program works fine. I'm really confused. I could turn it in just like that, but I want to know what's going wrong with the while loop..

Update: then I remove the extra space at the end and for the fifth employee I get a NoSuchElementException: No Line Found

[ February 08, 2006: Message edited by: Jesse Crockett ]
[ February 08, 2006: Message edited by: Jesse Crockett ]
Jeremy Tartaglia
Ranch Hand

Joined: Mar 11, 2004
Posts: 62
Sounds to me as if the data file and the algorithm don't match 100%. Try adding a new line to the end of the file:

And use the latter of your two algorithms. Or, just replace your scan.nextLine(); with if (scan.hasNextLine()) scan.nextLine();

I think one of those two should fix the problem.
[ February 08, 2006: Message edited by: Jeremy Tartaglia ]
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
Here is the program. The previous problem was somehow fixed, and it's working now. This is supposed to print a chart with each employee's gross and net pay, then print a tally line with the total gross and net pay to all employees. It is printing "0.00" for each employees gross and net, but the total line is printing the correct total gross and net. I think there is a problem in main() between the methods processPay() and printEmployeeInfo(). What's the problem here?

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

The problem here has to do with the scope of your variables. grossAmount is a local variable in this method. It is a copy of the grossAmount value in your main method, but the two variables are not the same. Therefore when you change grossAmount in this method, the grossAmount variable in your main method does not change. The workaround for this is either to declare class level static variables, or better yet write static methods return a value (and not void) and assign variables to those values in your main method

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
I agree. Here's the link:
subject: problem with StringBuffer and Double
jQuery in Action, 3rd edition