Reputation: 15
I am sort of a Newbie to this forum and to Java. I am having a difficult time trying to find a way to ask the user to enter more than one loan to compare from steps D down. I need to be able to ask the user for a different interest rate and number of years for the amount they entered in step A. So if they entered 10 then I would have to ask them 10 times for an interest rate and years and output it in a table format using tabs. Any help is much appreciated. Thanks in advance.
Edit: Thank you so much for your help! I updated the code.
//A. Enter the Number Of Loans to compare
String numberOfLoansString = JOptionPane.showInputDialog("Enter the amount of loans to compare:");
//Convert numberOfLoansString to int
int numberOfLoans = Integer.parseInt(numberOfLoansString);
//B. Enter the Amount/Selling Price of Home
String loanAmountString = JOptionPane.showInputDialog("Enter the loan amount:");
//Convert loanAmountString to double
double loanAmount = Double.parseDouble(loanAmountString);
//C. Enter the Down Payment on the Home
String downPaymentString = JOptionPane.showInputDialog("Enter the down payment on the Home:");
double downPayment = Double.parseDouble(downPaymentString);
//D. Ask the following for as many number of loans they wish to compare
//D1 Get the interest rate
double[] anualInterestRatesArray = new double[numberOfLoans];
double[] monthlyInterestRateArray = new double[numberOfLoans];
int[] numberOfYearsArray = new int[numberOfLoans];
double[] monthlyPaymentArray = new double[numberOfLoans];
double[] totalPaymentArray = new double[numberOfLoans];
for (int i=0; i < numberOfLoans; i++)
{
String annualInterestRateString = JOptionPane.showInputDialog("Enter the interest rate:");
double annualInterestRate = Double.parseDouble(annualInterestRateString);
anualInterestRatesArray[i] = (annualInterestRate);
//Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
monthlyInterestRateArray[i] = (monthlyInterestRate);
//D2 Get the number of years
String numberOfYearsString = JOptionPane.showInputDialog("Enter the number of years:");
int numberOfYears = Integer.parseInt(numberOfYearsString);
numberOfYearsArray[i] = (numberOfYears);
//Calculate monthly payment
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
//Format to keep monthlyPayment two digits after the decimal point
monthlyPayment = (int)(monthlyPayment * 100) / 100.0;
//Store monthlyPayment values in an array
monthlyPaymentArray[i] = (monthlyPayment);
//Calculate total Payment
double totalPayment = monthlyPaymentArray[i] * numberOfYears * 12;
//Format to keep totalPayment two digits after the decimal point
totalPayment = (int)(totalPayment * 100) / 100.0;
totalPaymentArray[i] = (totalPayment);
}
Upvotes: 0
Views: 348
Reputation: 103135
You probably need to use arrays and loops. Use arrays to store all the values entered and a loop to get the values.
double[] anualInterestRates = new double[numberOfLoans];
double[] monthlyInterestRates = new double[numberOfLoans];
int[] numberOfyears = new int[numberOfLoans];
Then you can loop and ask for each loan values:
for(int i= 0; i < numberOfLoans; i++){
//get the anual interest rate
anualInterestRates[i] = the anual interets rate gotten
//etc
}
Now you have 3 arrays of values. You can use a second loop to calculate the output and display.
Upvotes: 0
Reputation: 5519
An example for using for loop
int numberOfLoans = Integer.parseInt(numberOfLoansString);
//section of code which shouldnt be repeated here outside the loop.
for( int i = 0; i < numberOfLoans ; i++ )
{
//Write Step D here , because you want it to be repeated
}
Upvotes: 0
Reputation: 46395
You need to do all the repeated processing logic inside a loop such as for( ... )
loop. Use an array to store different values for the number of loans.
Upvotes: 2
Reputation: 13413
Use for loops for this.
P.S : You can use other loops [while, do-while] as well.
Upvotes: 1