• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with StringBuffer and Double

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic