-4

Part B: For Loop Program

Write a program to compute the interest on a bank account. The program will have the following characteristics:

  • The user will be prompted to input a single line of text containing the deposit amount ($), interest rate (%), and term (in months or years). All items must be separated by whitespace and the input should not be case sensitive. Interest must be compounded monthly. So, some sample (valid) inputs might be:
  • 10000.00 5 36 months
  • 5000.00 4.5 2 years
  • 45000.00 5.0 24 Months
  • If the user has made an input error, you need to inform them of the error and prompt them to re-enter the information. Primary error conditions are:
  • Term specified that is not “months” or “years”.
  • If values for interest rate or term time ≤ 0.
  • For this program, you must use the FOR loop construct to compute the interest.
  • You must make sure that interest is computed properly for monthly compounding. If you don’t know what “compound interest” is, Google it. One such site is here.
  • Once you have verified that the data is properly entered, compute and print out the interest payment on a month by month basis.
  • At the end of the program, print out the beginning balance, final balance, and the cumulative interest earned. Make sure to clearly label each output.

The code written so far is:

import java.util.Scanner; 

public class CompoundInterest { 

    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in); 

        double principal = 0; 
        double rate = 0; 
        double time = 0; 

        double compoundInterest = 0; 

        System.out.print("Enter the Principal amount : "); 
        principal = input.nextDouble(); 

        System.out.print("Enter the Rate : "); 
        rate = input.nextDouble(); 

        System.out.print("Enter the Time : "); 
        time = input.nextDouble(); 

        compoundInterest = principal * Math.pow((1 + rate/100), time); 

        System.out.println(""); 
        System.out.println("The Compound Interest is : " 
                          + compoundInterest); 
    } 
}

But I don't know how to get input from the user.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
Beginner
  • 1
  • 1
  • you should show your code and what you have tried. – smushi Oct 31 '14 at 01:24
  • Dude, we are happy to help, but not do your homework for you. Make a start, try writing some code. If you have a problem with the code your've written try asking a specific question. See http://stackoverflow.com/help/how-to-ask – David Waters Oct 31 '14 at 01:24
  • Is the question you're actually asking just how to capture input from the user? Because you went into an incredible amount of detail about parts of your project that isn't relevant, if that's all you need help with. It's like you explained to us in detail the entire recipe of a cake you want to make, step by step, and then ended the question by saying "but I'm out of flour, do you know where the nearest grocery store is?". – Sam Hanley Oct 31 '14 at 02:13
  • I want to know how read multiply inputs that are in one line. `System.out.println("Please input the following: principal, interest rate, term >> 1000 5 54 months")` I want to save principal,interest and rate from the one line input. – Beginner Oct 31 '14 at 02:42

1 Answers1

1

You could use the Scanner nextLine() method to read in an entire line of input from the user at once, but then you'd have to tokenize that line and convert each token to the correct data type. It's easier to code and more clear to the user if you ask for one input at a time the way you have it.

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842