1. Member Join Date
Jan 2012
Posts
3
Rep Power
0

## Menu-driven program

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.

Java Code:
```import java.util.*;
import java.text.DecimalFormat;

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;

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");

// 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!
Last edited by Link; 01-24-2012 at 12:01 PM.  Reply With Quote

2. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Menu-driven program

You're doing all your calculations before you have got any data from the user, so all the sums are done based on the default values you gave the variables (usually 0).  Reply With Quote

3. Member Join Date
Jan 2012
Posts
3
Rep Power
0

## Re: Menu-driven program

Awesome, I'm finally getting real numbers!
I took out the zeros behind my variables but I'm still not getting the right answers.
Thanks for getting me started though.  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
•