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 8 of 8

Thread: Getting confused need to add a for loop

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting confused need to add a for loop

    hi Im writing this program its a mortgage calculator. It works by entering data for each question such as down payment. My instructor wants us to change the code with a for loop that calculates 0,10,20...100% of the price of the house for a down payment. My code has several if statements for the questions asked. Im not sure how to add the for loop that increments by ten %
    my code is as follows
     
    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    	public class Project1 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		System.out.println("Enter the down payment: ");
    		double downPayment = 0;//input.nextDouble();   initial entry of downpayment 
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
     
     
    		double formulaTop = ((housePrice - downPayment) * (annualInterestRate/12));
    		double formulaBottom = ((1-(Math.pow(1+(annualInterestRate/12), -numberofPayments))));
    		double monthlyPayment = (formulaTop / formulaBottom);
    		DecimalFormat df = new DecimalFormat("0.00");
    		double endAmount = 0;
    		if (housePrice > 0){
    		System.out.println( "The price of the house is: $ " + (df.format(housePrice)));
     
    		}
    		else {
    			System.out.println("Error: Price must be greater than 0! ");
    		}
    		//trouble spot
    		for (double myPercent = 0; myPercent >=10; myPercent++) ///where im stuck for loop to calculate 11 cases
    																			//	(0-100%) of the house price
    			endAmount=(downPayment+myPercent);
    		if (downPayment > 0){                                  // downpayment if statement
    		System.out.println( "Your down payment is : $ " + (df.format(endAmount)));
    		}
     
     
    		else {
    			System.out.println("Error: Price must be greater than 0! ");
    		}
     
    		if (annualInterestRate < .025 ){
    			System.out.println("Error: Interest rate too low! ");
    		}
    			else if  (annualInterestRate > .05 ){
    				System.out.println("Error: Interest rate too high! ");
    		}
     
    			else {
    				System.out.println( "Your annual interest rate is :  " +(df.format(annualInterestRate)));
    			}		
    		if (numberofPayments > 0 ){
     
    		System.out.println( "Your number of payments are: " + (int)numberofPayments);	
    		}
    		else {
    			System.out.println( "Error: must be higher than 0");	
    		}
     
    		System.out.println( "Your monthly payment is: $ " + (df.format(monthlyPayment)));
    		System.exit(0);
     
     
    	}	
    	}

    this is the most code in a program ive written so far and im getting confused pleasse help


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Getting confused need to add a for loop

    I think the problem here is a misconception that the counter has to be incremented by 1 at the end of the loop. It was a huge revelation for me when i realized this was not required.

    for(double myPercent = 0; myPercent>=10; myPercent++)
    1. myPercent>=10 --Your loop will never start. 0 is less than 10 so it wont even start. This is your break condition. "I want to continue looping until this condition is met". I think you want something like myPercent <= 100. Then it will continue looping until you get over 100%.
    2. myPercent++ --This increments the counter by only 1. Basically it is "myPercent = myPercent + 1". So just change it to increment by 10 instead of 1. "myPercent = myPercent + 10".

    This may blow your mind, but you can do anything you want in these sections of the for loop as long as the middle section results in a boolean. Here's how it works:

    for(A;B;C)
    A: Executed first thing when you enter the loop. Most commonly used to declare a counter for the loop.
    B: Executed at the beginning of each pass. If it results in TRUE then it enters the loop. If it results in FALSE then it skips over the loop and continues with the program.
    C: Executed at the end of each pass. Most commonly used to increment the counter by some value.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting confused need to add a for loop

    thanks that help alot!! so a few things i dont know how to change the increment like you said to 10 i tried
    or (double myPercent = 0; myPercent <=100; myPercent+10 ){
    and also tried adding a myPercent = myPercent=10 but that didnt work, however i did this instead and it works
     or (double myPercent = 0; myPercent <=10; myPercent++ ){
    i know that theres a way to do what you said but i dont know it currently.
    here is my current code it has a few changes and it seems to work correctly.
     
    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    	public class Project1 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		//System.out.println("Enter the down payment: ");
    		double downPayment = 0;//input.nextDouble();   initial entry of downpayment 
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
     
     
    		double formulaTop = ((housePrice - downPayment) * (annualInterestRate/12));
    		double formulaBottom = ((1-(Math.pow(1+(annualInterestRate/12), -numberofPayments))));
    		double monthlyPayment = (formulaTop / formulaBottom);
    		DecimalFormat df = new DecimalFormat("0.00");
     
    		if (housePrice > 0){
    		System.out.println( "The price of the house is: $ " + (df.format(housePrice)));
     
    		}
    		else {
    			System.out.println("Error: Price must be greater than 0! ");
    		}
     
     
    		for (double myPercent = 0; myPercent <=10; myPercent++ ){ 
     
    				downPayment=((myPercent*.1)*housePrice);
    		//if (downPayment > 0){                                  // downpayment if statement
    		System.out.println( "Your down payment is : $ " + (df.format(downPayment)));
    		}
     		//else {
    			//System.out.println("Error: Price must be greater than 0! ");
    		//}
     
    		if (annualInterestRate < .025 ){
    			System.out.println("Error: Interest rate too low! ");
    		}
    			else if  (annualInterestRate > .05 ){
    				System.out.println("Error: Interest rate too high! ");
    		}
     
    			else {
    				System.out.println( "Your annual interest rate is :  " +(df.format(annualInterestRate)));
    			}		
    		if (numberofPayments > 0 ){
     
    		System.out.println( "Your number of payments are: " + (int)numberofPayments);	
    		}
    		else {
    			System.out.println( "Error: must be higher than 0");	
    		}
     
    		System.out.println( "Your monthly payment is: $ " + (df.format(monthlyPayment)));
    		System.exit(0);
     
     
    	}	
    	}

    Im stuck on adding a for loop to make the monthly payment calculate 11 payments based on the 11 reults returned from the downPayment loop.

    Im not sure how or where to set this loop up

    --- Update ---

    revised code again modifed formula and combined it to one line, removed temp variables associated with the formula.
     
    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    	public class Project1 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		//System.out.println("Enter the down payment: ");
    		double downPayment = 0;//input.nextDouble();   initial entry of downpayment 
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
     
     
    		double monthlyPayment = (((housePrice - downPayment) * (annualInterestRate/12))/((1-(Math.pow(1+(annualInterestRate/12), -numberofPayments)))));
    		//double formulaBottom = ((1-(Math.pow(1+(annualInterestRate/12), -numberofPayments))));
    		//double  = (formulaTop / formulaBottom);
    		DecimalFormat df = new DecimalFormat("0.00");
     
    		if (housePrice > 0){
    		System.out.println( "The price of the house is: $ " + (df.format(housePrice)));
     
    		}
    		else {
    			System.out.println("Error: Price must be greater than 0! ");
    		}
     
     
    		for (double myPercent = 0; myPercent <=10; myPercent++ ){ 
     
    				downPayment=((myPercent*.1)*housePrice);
    		//if (downPayment > 0){                                  // downpayment if statement
    		System.out.println( "Your down payment is : $ " + (df.format(downPayment)));
    		}
     		//else {
    			//System.out.println("Error: Price must be greater than 0! ");
    		//}
     
    		if (annualInterestRate < .025 ){
    			System.out.println("Error: Interest rate too low! ");
    		}
    			else if  (annualInterestRate > .05 ){
    				System.out.println("Error: Interest rate too high! ");
    		}
     
    			else {
    				System.out.println( "Your annual interest rate is :  " +(df.format(annualInterestRate)));
    			}		
    		if (numberofPayments > 0 ){
     
    		System.out.println( "Your number of payments are: " + (int)numberofPayments);	
    		}
    		else {
    			System.out.println( "Error: must be higher than 0");	
    		}
     
    		System.out.println( "Your monthly payment is: $ " + (df.format(monthlyPayment)));
    		System.exit(0);
     
     
    	}	
    	}

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Getting confused need to add a for loop

    What you have for your for loop works but is bad format. You were close, just had a bit of a syntax issue. Here's what you're looking for in two different ways.

    for (double myPercent = 0; myPercent <=100; myPercent+=10 ){ //This version does the += operator. This operator adds the variable being assigned to the answer of the equation to the right (variable with value 2) += 5-3 would result in 4. Make sense?
    for (double myPercent = 0; myPercent <=100; myPercent = myPercent + 10 ){ //This version shows it without any short cuts. If you have any exceptions or errors showing, try putting .0 at the end of your values like 10.0 or 100.0. Sometimes doubles dont like working with ints.

    As for your second portion, i'm not really sure what you're getting at. You need to calculate the monthly payment for each possible down payment? If this is true, you could either put the calculation directly in the for loop that is getting the down payment, or if you really want to be professional, save the values of the down payments to a list then loop through the list in another for loop to calculate each monthly payment.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting confused need to add a for loop

    thanks again
    heres what i got currently
     
    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    	public class Project1 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		//System.out.println("Enter the down payment: ");
    		double downPayment = 0;//input.nextDouble();   initial entry of downpayment 
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
     
     
     
    		//double formulaBottom = ((1-(Math.pow(1+(annualInterestRate/12), -numberofPayments))));
    		//double  = (formulaTop / formulaBottom);
    		DecimalFormat df = new DecimalFormat("0.00");
     
    		if (annualInterestRate < .025 ){
    			System.out.println("Error: Interest rate too low! ");
    		}
    			else if  (annualInterestRate > .05 ){
    				System.out.println("Error: Interest rate too high! ");
    		}
     
    			else {
    				System.out.println( "Your annual interest rate is :  " +(df.format(annualInterestRate)));
    			}		
    		if (numberofPayments > 0 ){
     
    		System.out.println( "Your number of payments are: " + (int)numberofPayments);	
    		}
    		else {
    			System.out.println( "Error: must be higher than 0");	
    		}
     
    		if (housePrice > 0){
    		System.out.println( "The price of the house is: $ " + (df.format(housePrice)));
    		System.out.println("");
    		}
    		else {
    			System.out.println("Error: Price must be greater than 0! ");
    		}
     
     
    		for (double myPercent = 0; myPercent <=100; myPercent+=10 ){ 										
    				downPayment=((myPercent*.01) * housePrice);
    				double monthlyPayment = (((housePrice - downPayment) * (annualInterestRate/12))/((1-(Math.pow(1+(annualInterestRate/12), -numberofPayments)))));
     
     
    		System.out.println( "Your down payment is : $ " + (df.format(downPayment)));
    		System.out.println( "Your monthly payment is: $ " + (df.format(monthlyPayment)));
    		System.out.println("");
    		}
     
     
    	}	
    	}

    please check

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Getting confused need to add a for loop

    I still recommend putting different pieces in different sections then doing all of the output at the end. I encourage you to give that a shot if you have the spare time. It will make you a better programmer in the end.

    Anyways, i ran your program and it all looks to be working for me. Now that you're done, save a copy then try to make it better.

    If you didnt want to bother doing the *.01 you could divide all of your numbers in the for loop by .01 instead. Just a thought.

    for (double myPercent = 0; myPercent <=1; myPercent+=0.1 ){
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting confused need to add a for loop

    Im not sure what you mean by different section, but Ill see what I can find. Your right they must be ways to display this info in a more professional manner
    thanks for your help its much appreciated!

  8. #8
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting confused need to add a for loop

    alright so the last thing i needed to do is rewrite to code so that i can enter values for each question and then have passed to a method for calculation and then passed backed for both to be printed out. Now it all seems to work but there are some exemptions i put in that are to print errors and stop the program. the ones in the annual interest rate if block dont work, they allow values above and below the parameters set to be calculated.
    heres my code
     
     
     
     
     
    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    	public class MethodVersion {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		double housePrice2 = input.nextDouble();
    		System.out.println("Enter the down payment: ");
    		double downPayment = input.nextDouble(); 
    		double downPayment2 = input.nextDouble(); 
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		double annualInterestRate2 = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
    		double numberofPayments2 = input.nextDouble();
    		System.out.println("");System.out.println("");
    		double monthlyPayments = MortgageMethod(housePrice, downPayment, annualInterestRate, numberofPayments );
    		double monthlyPayments2 = MortgageMethod(housePrice, downPayment, annualInterestRate, numberofPayments );
     
    		DecimalFormat df = new DecimalFormat("0.00");
     
     
    		if (housePrice > 0 && housePrice2 > 0){
    			System.out.println( "The price of the house1 is: $ " + (df.format(housePrice)));
    			System.out.println( "The price of the house2 is: $ " + (df.format(housePrice2)));
    			}
    			else {
    				System.out.println("Error: Price must be greater than 0! ");
    				System.exit(0);
    			}
    		System.out.println("");
    		if (downPayment > 0 && downPayment2 > 0){
    			System.out.println( "Your 1st down payment would be: $ " + (df.format(downPayment)));
    			System.out.println( "Your 2nd down payment would be: $ " + (df.format(downPayment2)));
    			}
    			else {
    				System.out.println("Error: Price must be greater than 0! "); //error message if under 1
    				System.exit(0);
    			}
    		System.out.println("");
     
    		if (annualInterestRate < .025 && annualInterestRate2 < .025 ){  //this block isnt working properly the if and th else if
    			                                                             //dont seem to work if you enter above or below the requirements it still runs the computation.
    			System.out.println("Error: Interest rate too low! ");
    			System.exit(0);
    		}
    			else if  (annualInterestRate > .05 && annualInterestRate2 > .05 ){
    				System.out.println("Error: Interest rate too high! ");
    				System.exit(0);
    		}
     
    			else {
    				System.out.println( "Your 1st annual interest rate would be:  " +(df.format(annualInterestRate)));
    				System.out.println( "Your 2nd annual interest rate would be:  " +(df.format(annualInterestRate2)));
    			}		
    		System.out.println("");
    		if (numberofPayments > 0 && numberofPayments2 > 0){
    			System.out.println( "Your 1st number of payments would be: " + (int)numberofPayments);	
    			System.out.println( "Your 2nd number of payments would be: " + (int)numberofPayments2);	
    			}
    			else {
    				System.out.println( "Error: must be higher than 0");	
    				System.exit(0);
    		}
     
    		System.out.println("");
    		System.out.println( "Your 1st monthly payment would be: $ " + (df.format(monthlyPayments)));
    		System.out.println( "Your 2nd monthly payment would be: $ " + (df.format(monthlyPayments2)));
    		}
     
     
    		public static double MortgageMethod(double tempHouse, double tempDownPayment,  double tempInterest, double tempNumberofPayments){
    			//for (double myPercent = 0; myPercent <=1; myPercent+=.1 ){ 										
    				//double downPayment=(myPercent * tempHouse);
     
     
    				double paymentPass = (((tempHouse - tempDownPayment) * (tempInterest/12))/((1-(Math.pow(1+(tempInterest/12), -tempNumberofPayments)))));
     
     
    				return paymentPass;
    		}
    	}
    please check

    --- Update ---

    fixed it needed to use or in stead of and in the if statement


     
     
    /**
    * Project 1-ReSubmittion
    * File: MethodVersion
    * Date of creation:3/25/2013
    * Date of modification: 3/25/2013
    * Author: Adam Gonia
    **/
     
     
    import java.text.DecimalFormat;  //allows for decimal formatting
    import java.util.Scanner;  //allows for user input
     
    	public class MethodVersion {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);// where user enters values
    		System.out.println("Please enter two values for each question: ");
    		System.out.println("");
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		double housePrice2 = input.nextDouble();
    		System.out.println("Enter the down payment: ");
    		double downPayment = input.nextDouble(); 
    		double downPayment2 = input.nextDouble(); 
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		double annualInterestRate2 = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
    		double numberofPayments2 = input.nextDouble();
    		System.out.println("");System.out.println("");
    		double monthlyPayments = MortgageMethod(housePrice, downPayment, annualInterestRate, numberofPayments );//the return of the method
    		double monthlyPayments2 = MortgageMethod(housePrice2, downPayment2, annualInterestRate2, numberofPayments2 );// the return of the method
    		double annualInterestRatePercent = (annualInterestRate * 100);
    		double annualInterestRatePercent2 = (annualInterestRate2 * 100);
    		DecimalFormat df = new DecimalFormat("0.00"); // decimal formatting change
     
    		//beginning of if statements
    		if (housePrice > 0 && housePrice2 > 0){
    			System.out.println( "The 1st price of the house is:$" + (df.format(housePrice)));
    			System.out.println( "The 2nd price of the house is: $" + (df.format(housePrice2)));
    			}
    			else {
    				System.out.println("Error: Price must be greater than 0! ");
    				System.exit(0);
    			}
    		System.out.println("");
    		if (downPayment > 0 && downPayment2 > 0){
    			System.out.println( "Your 1st down payment would be: $" + (df.format(downPayment)));
    			System.out.println( "Your 2nd down payment would be: $" + (df.format(downPayment2)));
    			}
    			else {
    				System.out.println("Error: Price must be greater than 0! "); //error message if under 1
    				System.exit(0);
    			}
    		System.out.println("");
     
    		if (annualInterestRate < .025 || annualInterestRate2 < .025 ){  //this block isnt working properly the if and th else if
    			                                                             //dont seem to work if you enter above or below the requirements it still runs the computation.
    			System.out.println("Error: Interest rate too low! ");
    			System.exit(0);
    		}
    			else if  (annualInterestRate > .05 || annualInterestRate2 > .05 ){
    				System.out.println("Error: Interest rate too high! ");
    				System.exit(0);
    		}
     
    			else {
    				System.out.println( "Your 1st annual interest rate would be: " +(df.format(annualInterestRatePercent))+" %");
    				System.out.println( "Your 2nd annual interest rate would be: " +(df.format(annualInterestRatePercent2))+" %");
    			}		
    		System.out.println("");
     
    		if (numberofPayments > 0 && numberofPayments2 > 0){
    			System.out.println( "Your 1st number of payments would be: " + (int)numberofPayments);	
    			System.out.println( "Your 2nd number of payments would be: " + (int)numberofPayments2);	
    			}
    			else {
    				System.out.println( "Error: must be higher than 0");	
    				System.exit(0);
    		}
     
    		System.out.println("");
    		System.out.println( "Your 1st monthly payment would be: $" + (df.format(monthlyPayments)));
    		System.out.println( "Your 2nd monthly payment would be: $" + (df.format(monthlyPayments2)));
    		}
     
    	//method MortgageMethod passes the input values so they can be calculated.
    		public static double MortgageMethod(double tempHouse, double tempDownPayment,  double tempInterest, double tempNumberofPayments){
     
    				double paymentPass = (((tempHouse - tempDownPayment) * (tempInterest/12))/((1-(Math.pow(1+(tempInterest/12), -tempNumberofPayments)))));
    				//formula used for payment
     
    				return paymentPass;
    		}
    	}
    //end

Similar Threads

  1. [SOLVED] Confused without a teacher on a loop.
    By Hazmat210 in forum Loops & Control Statements
    Replies: 16
    Last Post: January 30th, 2013, 10:03 PM
  2. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 12:28 PM
  3. Replies: 1
    Last Post: April 26th, 2011, 08:47 AM
  4. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 06:45 AM
  5. confused on loop
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2010, 02:26 PM