Results 1 to 3 of 3
  1. #1
    HarrySingh is offline Member
    Join Date
    Oct 2015
    Posts
    1
    Rep Power
    0

    Default 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
    Help Me In Getting The Correct Output-java-q.jpg
    My code is :

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * 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 :

    Help Me In Getting The Correct Output-java-program-output.jpg


    Please help me in getting the correct output!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Not getting correct output.
    By sithclone3 in forum New To Java
    Replies: 33
    Last Post: 06-29-2014, 08:29 PM
  2. Getting correct output
    By WarmRegards in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 04:41 PM
  3. Is this the correct Output?
    By Teny in forum New To Java
    Replies: 17
    Last Post: 04-13-2009, 12:52 PM
  4. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 12:22 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

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