Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Loan Repayment

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Loan Repayment

    		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.
    Last edited by Langolier; February 18th, 2012 at 11:08 AM. Reason: edited code


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)

  3. The Following User Says Thank You to copeg For This Useful Post:

    Langolier (February 17th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  5. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Loan Repayment

    Anyone please help me?

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Loan Repayment

     
     
    	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
    Last edited by Langolier; February 18th, 2012 at 12:59 PM.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Loan Repayment

    it really wont work
    Please explain.
    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.

  8. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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

  10. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Loan Repayment

     
    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.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Loan Repayment

    Congratulations.

Similar Threads

  1. Amortization Loan, works, but need suggestions please.
    By TheWhopper858 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 03:38 AM
  2. Loan Payments
    By clarafeb1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2011, 03:36 AM
  3. savings and loan society
    By 5723 in forum Java Theory & Questions
    Replies: 9
    Last Post: November 14th, 2009, 12:01 AM