• 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

printf problems

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to use a printf method and I am having troubles getting my printout to work right. Below is what my printout is returning upon compilation.




C:\Documents and Settings\ben\Desktop\park>java BenHultinProg4
Month Account # Balance Account #
----- --------- ------- ---------
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003
0 10002 2000.0 10003

C:\Documents and Settings\ben\Desktop\park>




here is what I am trying to achieve:


Monthly balances for one year with 0.05 annual interest:

// Month Account # Balance Account # Balance
// ----- --------- ------- --------- -------
// 0 10002 2000.00 10003 3000.00
// 1 10002 2008.33 10003 3012.50
// 2 10002 2016.70 10003 3025.05
// 3 10002 2025.10 10003 3037.66
// 4 10002 2033.54 10003 3050.31
// 5 10002 2042.02 10003 3063.02
// 6 10002 2050.52 10003 3075.79
// 7 10002 2059.07 10003 3088.60
// 8 10002 2067.65 10003 3101.47
// 9 10002 2076.26 10003 3114.39
// 10 10002 2084.91 10003 3127.37
// 11 10002 2093.60 10003 3140.40
// 12 10002 2102.32 10003 3153.49



Driver file:




driven file:




I appreciate any help in the matter
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
just started and gota run, but the HEADING_FMT_STR and DATA_FMT_STR do not account for the Balance field / underline for the second account.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the Javadoc of java.util.Formatter, especially the %d / %f parts.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe your formatting should be sorted out with the change

final String HEADING_FMT_STR = "%-13s%13s%13s%13s%15s\n";
final String DATA_FMT_STR = "%-13s%13s%13.2f%13s%15.2f\n";

I also notice that you are putting out 0 in first column rather than i.

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the pointer. I made the changes to HEADING_FMT_STR and DATA_FMT_STR and now my output is including the last balance column.



C:\Documents and Settings\ben\Desktop\park>java BenHultinProg4
Month Account # Balance Account # Balance
----- --------- ------- --------- -------
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0
0 10002 2000.0 10003 3000.0

C:\Documents and Settings\ben\Desktop\park>

 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have missed out two decimal places and the letter f in your data format. Check my format again. As suggested earlier look at the documentation for the Formatter class.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So now I have my formatting set up, thanks for the help there. Now I just need for the addMonthlyInterest() to do its job and send the results to balance for printing.


C:\Documents and Settings\ben\Desktop\park>java BenHultinProg4
Month Account # Balance Account # Balance
----- --------- ------- --------- -------
0 10002 2000.00 10003 3000.00
1 10002 2000.00 10003 3000.00
2 10002 2000.00 10003 3000.00
3 10002 2000.00 10003 3000.00
4 10002 2000.00 10003 3000.00
5 10002 2000.00 10003 3000.00
6 10002 2000.00 10003 3000.00
7 10002 2000.00 10003 3000.00
8 10002 2000.00 10003 3000.00
9 10002 2000.00 10003 3000.00
10 10002 2000.00 10003 3000.00
11 10002 2000.00 10003 3000.00
12 10002 2000.00 10003 3000.00

C:\Documents and Settings\ben\Desktop\park>



I appreciate the help
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch, Adam Smith
 
adam smith ii
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,
The program has 2 declarations for annualInterestRate. The one in SavingsAccount class has a value of 0.0 and is the one that is used by method addMonthlyInterest ().

A.S.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the help everyone, I was able to get it working completly by just changing

annualInterestRate from 0.0 to 0.05 which is the interest rate I am looking for.

Thanks again
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic