wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes printf problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "printf problems" Watch "printf problems" New topic
Author

printf problems

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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
adam smith ii
Greenhorn

Joined: Feb 04, 2010
Posts: 20
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Check out the Javadoc of java.util.Formatter, especially the %d / %f parts.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
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.


Rajeev Trikha (SCJP 6)
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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

Joined: Jan 29, 2010
Posts: 85
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

Joined: Aug 17, 2009
Posts: 135
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
And welcome to the Ranch, Adam Smith
adam smith ii
Greenhorn

Joined: Feb 04, 2010
Posts: 20
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

Joined: Aug 17, 2009
Posts: 135
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
 
wood burning stoves
 
subject: printf problems
 
Similar Threads
Compound Interest on an object
illegal start of an expression
Calculating a total after a "for" loop
SubClass Blues!
Very Confused!!