Loan Repayment

• February 17th, 2012, 02:07 PM
Langolier
Loan Repayment
Code :

``` import java.util.Scanner; // import scanner   public class Loan {   static Scanner input = new Scanner(System.in); // create scanner object usable in all methods   public static void main(String[] args) {   // Declarations int cont = 1; double loanAmount = 1; double interest = 1.1; double numberYears = 1; double monthlyInterest = interest/1200; //endDeclarations   System.out.println("Welcome to the loan Calculator. I will help you calculate the monthy payments on a loan"); //welcome statement   System.out.print("Would you like to calculate loan payments now? Please enter 1 for Yes anything else for No: "); //loop variable input cont = input.nextInt();   while(cont == 1){ // begin while loop //begin collecting data for variables System.out.print("Please enter the loan amount in dollars and cents. Example: \$1500.75:\$ "); loanAmount = input.nextDouble();   System.out.print("Please enter the interest rate. Example: 5.7%:% "); interest = input.nextDouble();   System.out.print("Please enter the number of years the loan will be for. Example: 15: "); numberYears = input.nextInt();     double monthlyRepayment = loanAmount* monthlyInterest/(1-(Math.pow(1/ (1+monthlyInterest), numberYears*12)));   System.out.println("The Monthly Payment: \$" + (int) (monthlyRepayment * 100 /100.0 ));   System.out.println("Would you like to calculate another Loan? Please enter 1 for Yes: "); cont = input.nextInt();   } // endwhile   System.out.println("Thanks for using the Loan Calculator!"); }     }```

I think my Math calculation is correct now. I just need help if anyone could with getting it into separate methods. I do not know why I am updating this since no one seems to want to help me anyhow. I just holding out hope.
• February 17th, 2012, 02:36 PM
copeg
Re: Loan Repayment
I recommend reading the following, and take careful note of where semicolons need to be placed.
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
• February 17th, 2012, 03:03 PM
Langolier
Re: Loan Repayment
Ok, I removed the bad semi colon. Thanks for the help. I guess my math calculation is wrong because when I run it I get a negative answer
• February 18th, 2012, 08:52 AM
Langolier
Re: Loan Repayment
• February 18th, 2012, 11:29 AM
Langolier
Re: Loan Repayment
Code :

```    import java.util.Scanner; // import scanner   public class Loan {   static Scanner input = new Scanner(System.in); // create scanner object usable in all methods       public static void main(String[] args) {   // Declarations int cont = 1; double loanAmount = 1; double interest = 1.1; double numberYears = 1; double monthlyInterest = interest/1200; double monthlyRepayment; double result = max(loanAmount, interest, numberYears); //endDeclarations   System.out.println("Welcome to the loan Calculator. I will help you calculate the monthly payments on a loan"); //welcome statement   System.out.print("Would you like to calculate loan payments now? Please enter 1 for Yes anything else for No: "); //loop variable input cont = input.nextInt();   while(cont == 1){ // begin while loop //begin collecting data for variables System.out.print("Please enter the loan amount in dollars and cents. Example: \$1500.75:\$ "); loanAmount = input.nextDouble();   System.out.print("Please enter the interest rate. Example: 5.7%:% "); interest = input.nextDouble();   System.out.print("Please enter the number of years the loan will be for. Example: 15: "); numberYears = input.nextInt();     System.out.println("The Monthly Payment: \$" +(int) (result*100/100));   System.out.println("Would you like to calculate another Loan? Please enter 1 for Yes: "); cont = input.nextInt();   }   System.out.println("Thanks for using the Loan Calculator!"); }   public static double max(double loanAmount, double monthlyInterest, double numberYears ){   double monthlyRepayment = loanAmount* monthlyInterest/(1-(Math.pow(1/ (1+monthlyInterest), numberYears*12)));   return monthlyRepayment;             } }```

Now it really wont work :(
• February 18th, 2012, 11:49 AM
Norm
Re: Loan Repayment
Quote:

it really wont work
Post the full text of the error messages
or show the output, explain what the problem is and show what the output should look like.
• February 18th, 2012, 12:02 PM
Langolier
Re: Loan Repayment
I guess in the second piece of code I posted trying to code the calculation method is wrong. It will only return a value of 1. I edited the above code to show what it now looks like. I think the code in the first post is what I am about to go back to. It is my base code. I just need to separate it into methods.
To be exact:
You will accept input in the main method. You will use a separate method (other than main) to calculate the payment. You will use a third method to output the result. The third method (output) can be called from either the main method or the calculation method.
• February 18th, 2012, 12:10 PM
Norm
Re: Loan Repayment
Can you explain what your problem is?

Post the program's output, explain what the problem is and show what the output should look like
• February 18th, 2012, 12:27 PM
Langolier
Re: Loan Repayment
Code :

```  import java.util.Scanner; // import scanner   public class Loan {   static Scanner input = new Scanner(System.in); // create scanner object usable in all methods   public static void main(String[] args) {   // Declarations int cont = 1; double loanAmount = 1; double interest = 1.1; double numberYears = 1; //endDeclarations   System.out.println("Welcome to the loan Calculator. I will help you calculate the monthly payments on a loan"); //welcome statement   System.out.print("Would you like to calculate loan payments now? Please enter 1 for Yes anything else for No: "); //loop variable input cont = input.nextInt();   while(cont == 1){ // begin while loop //begin collecting data for variables System.out.print("Please enter the loan amount in dollars and cents. Example: \$1500.75:\$ "); loanAmount = input.nextDouble();   System.out.print("Please enter the interest rate. Example: 5.7%:% "); interest = input.nextDouble();   System.out.print("Please enter the number of years the loan will be for. Example: 15: "); numberYears = input.nextInt();   double monthlyRepayment = getMonthlyPayment(loanAmount, interest, numberYears); displayOutput(monthlyRepayment);   System.out.println("Would you like to calculate another Loan? Please enter 1 for Yes: "); cont = input.nextInt();   } // endwhile   System.out.println("Thanks for using the Loan Calculator!"); }   public static double getMonthlyPayment(double loanAmount, double interest, double numberYears) { double monthlyInterest = interest/1200; double monthlyRepayment = loanAmount* monthlyInterest/(1-(Math.pow(1+monthlyInterest, -numberYears*12))); return monthlyRepayment; }   public static void displayOutput(double monthlyRepayment) { System.out.printf("The Monthly Payment: \$%,.2f\n", monthlyRepayment); } }```

This is working. Thanks for trying to help though. I just need to add my comments. This is as close as I am gonna get after spending way too long on it.
• February 18th, 2012, 12:29 PM
Norm
Re: Loan Repayment
Congratulations.