• 01-24-2012, 11:35 AM
Hi, I'm supposed to write a loan calculator menu-driven program.
The user inputs the payment amount, annual interest rate (which needs to be converted to months), and the loan period in months.

Below is what I've written so far but my program outputs all the wrong numbers.
When I try to calculate the PAYMENT it always spits out -\$0.00, the INTEREST just returns what I input as the interest rate, and the TOTAL is always \$0.00.

Code:

```import java.util.*; import java.text.DecimalFormat; public class Menu {         public static void main(String[] args) {                 // initialize money format                 DecimalFormat money = new DecimalFormat("\$0.00");                                 // variables                 String input;             char inputCode = ' ' ;             double PRINCIPLE = 0;                 double RATE = 0;                 int TERM = 0;                 // equations                 double PAYMENT = PRINCIPLE * ((RATE / 1200) / 1 - Math.pow(1 + (RATE / 1200), -TERM));                 double INTEREST = TERM * PAYMENT - PRINCIPLE;                 double TOTAL = PRINCIPLE + INTEREST;                             /* menu */                 System.out.println("Enter [p] for payment amount");                 System.out.println("Enter [i] for interest rate");                 System.out.println("Enter [t] for total");                 System.out.println("Enter [q] to terminate program");                 System.out.print("\nPlease enter your choice: ");                         // create Scanner object for keyboard input                 Scanner sc = new Scanner(System.in);             input = sc.nextLine();             inputCode = input.charAt(0);                         while ((inputCode != 'p' && inputCode != 'i' && inputCode != 't' && inputCode !='q') &&                       (inputCode != 'P' && inputCode != 'I' && inputCode != 'T' && inputCode !='Q')) {               System.err.println("Invalid choice! Please select p, i, t, or q: ");               input = sc.nextLine();               inputCode = input.charAt(0);             }                       /* display results */             switch (inputCode) {                     case 'p':                     case 'P':                             System.out.print("\nEnter principle (loan amount), e.g. 120000.95: ");                             PRINCIPLE = sc.nextDouble();                             System.out.print("Enter annual interest rate (between 1-100), e.g. 5.25: ");                             RATE = sc.nextDouble();                             if (RATE < 1 || RATE > 100) {                                 System.err.println("Invalid input!  Please re-enter annual interest rate: ");                                 RATE = sc.nextDouble();                             }                             System.out.print("Enter term (loan period) in months as an integer, e.g. 8: ");                             TERM = sc.nextInt();                             System.out.println("\nThe payment amount is " + money.format(PAYMENT));                             break;                     case 'i':                     case 'I':                             System.out.print("\nEnter (principle) loan amount, e.g. 125000.00: ");                             PRINCIPLE = sc.nextDouble();                             System.out.print("Enter annual interest rate (between 1-100), e.g. 5.25: ");                             RATE = sc.nextDouble();                             if (RATE < 1 || RATE > 100) {                                 System.err.println("Invalid input!  Please re-enter annual interest rate: ");                                 RATE = sc.nextDouble();                             }                             System.out.print("Enter term (loan period) in months as an integer , e.g. 8: ");                             TERM = sc.nextInt();                             System.out.println("\nThe interest rate is " + RATE);                             break;                     case 't':                     case 'T':                             System.out.print("\nEnter (principle) loan amount, e.g. 125000.00: ");                             PRINCIPLE = sc.nextDouble();                             System.out.print("Enter annual interest rate (between 1-100), e.g. 5.25: ");                             RATE = sc.nextDouble();                             if (RATE < 1 || RATE > 100) {                                 System.err.println("Invalid input!  Please re-enter annual interest rate: ");                                 RATE = sc.nextDouble();                             }                             System.out.print("Enter term (loan period) in months as an integer, e.g. 8: ");                             TERM = sc.nextInt();                             System.out.println("\nThe total amount is " + money.format(TOTAL));                             break;                     case 'q':                     case 'Q':                             System.out.println("Program terminated");             }                     } }```
Can anyone help me find where I went wrong?

I know it's terrible that I'm struggling with basic concepts but I've never taken a programming course before and this class also has a prereq that I didn't take. I best learn through example so any help or advice to make my code more efficient will be appreciated!
• 01-24-2012, 11:40 AM
Tolls