Odd Output in Java (noobie error most likely)
Question
This will be my second question on here; I try not to ask because I don't fancy receiving help for my homework. But I'm just plain stuck.
I'm currently writing a program that stores and updates information for some example bank accounts. My issue is that, somehow, my output has two decimal points.
Example: "2000.02008.3333333333333"
Side notes: I'm in a Tier II java course, so yes, my code isn't the prettiest. Also, please only offer hints as to where I should make changes. As I said earlier, I try really hard not to ask for help and I don't want my request for help to be misconstrued as cheating. Finally, I do know about syso formatting and will be doing that with the Title information in my driver. The way I did it in there was just faster for me at the time while I fleshed out the rest of the details.
Many thanks in advance to any help provided!
Here's my current code:
public class SavingsAccount
{
private static double annualInterestRate;
private final int ACCOUNT_NUMBER;
private double balance;
public SavingsAccount(int ACCOUNT_NUMBER, double balance)
{
this.ACCOUNT_NUMBER = ACCOUNT_NUMBER;
this.balance = balance;
}
public static double setAnnualInterestRate(double aIR)
{
annualInterestRate = aIR;
return annualInterestRate;
}
public int getAccountNumber()
{
return ACCOUNT_NUMBER;
}
public double getBalance()
{
return balance;
}
public double addMonthlyInterest()
{
balance += (balance * annualInterestRate / 12);
return balance;
}
}
DRIVER
public class Chapter13
{
public static void main(String[] args)
{
double annualInterestRate = .05;
SavingsAccount saver1 = new SavingsAccount(10002, 2000);
SavingsAccount saver2 = new SavingsAccount(10003, 3000);
SavingsAccount.setAnnualInterestRate(annualInterestRate);
System.out.println("Month Acount# Balance Account# Balance");
for(int i = 0; i < 13; i++)
{
System.out.println(i + " " + saver1.getBalance() +
saver1.addMonthlyInterest());
}
}
}
Solution
That's because the balance
and annualInterestRate
are getting printed without any space between them. Add a tab or space between them so that they are printed separately.
System.out.println(i + " " + saver1.getBalance() + "\t" // tab
saver1.addMonthlyInterest());
System.out.println(i + " " + saver1.getBalance() + " " // space
saver1.addMonthlyInterest());