# Thread: Help Me In Getting The Correct Output

1. Member Join Date
Oct 2015
Posts
1
Rep Power
0

## Help Me In Getting The Correct Output

Write an application that calculates the amount of money earned on an investment invested for a given period, based upon the annual rate of interest entered by the user.
The amount of investment must be positive and can be any value.
The period of investment is in years so should be positive.
The annual rate of interest can be between 0.25% to 10%.
Your application should validate each data entered by the user and if the value entered is invalid then after displaying an appropriate error message it should continue to ask the user for correct value.
The amount of money earned can be calculated using the following formulae and it is paid monthly:
Money earned = invested amount * rate of interest My code is :

Java Code:
```/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package interest.calculator;

/**
*
* @author
*/

import java.util.Scanner;

public class InterestCalculator{

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

Scanner input = new Scanner(System.in);

// Entering the interest rate
double annualInterestRate = 0;
while (annualInterestRate < 0.25 || annualInterestRate > 10){
System.out.print("Please Enter the annual interest rate between 0.25 to 10 : ");
annualInterestRate = input.nextDouble();
if (annualInterestRate < 0.25 || annualInterestRate > 10){
System.out.println("ERROR. You Have Not Entered Interest Rate Between 0.25 and 10");
}
}

double monthlyInterestRate = annualInterestRate / 12;

int numberOfYears = 0;
while (numberOfYears <= 0){
System.out.print("Please Enter number of years in Positive: ");
numberOfYears = input.nextInt();
if (numberOfYears <= 0){
System.out.println("ERROR. The Number Of Years Entered Is Not Positive");
}

}

// Entering the amount earned
double Amountofinterest = 0;
while (Amountofinterest <= 0){
System.out.print("Please Enter The Amount In Positive: ");
Amountofinterest = input.nextDouble();
if (Amountofinterest <= 0){
System.out.println("ERROR. The Amount Entered Is NOT Positive");
}
}

// Calculating
double moneyearned = Amountofinterest * monthlyInterestRate;

// Displaying the results
System.out.println("The money earned is \$" +
(int)(moneyearned * 100) / 100.0);
int i;

for (i = 1; i <= numberOfYears * 12; i++) {
double Balance = Amountofinterest + moneyearned;
Amountofinterest = Balance;
monthlyInterestRate = moneyearned + 0.01;
System.out.println(i + "\t\t" + Amountofinterest
+ "\t\t" + monthlyInterestRate + "\t\t" + Balance);

}

}```

My Output is :   Reply With Quote

2. ## Re: Help Me In Getting The Correct Output

You shouldn't try to use tabs (\t) to get the format right (see your result); use the System.out.printf( ... ) method instead with the correct width specifiers.

kind regards,

Jos  Reply With Quote

3. ## Re: Help Me In Getting The Correct Output

Also see the API doc for the Formatter class for how to code the format Strings used to control the display of the numbers when they are printed.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•