Java Program help with loops

the 2nd loop does not run

Page 1 of 1

4 Replies - 1580 Views - Last Post: 12 May 2008 - 10:20 PM Rate Topic: -----

#1 mistymoon1966   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Java Program help with loops

Posted 12 May 2008 - 04:22 PM

I had to create a program that had 3 different mortgage information with a loop and then the amortization of each mortgage with a loop. I already handed the program in that does not work and class is over. However, I have another Java class in 1 week and would like to fix my program before that class starts. The first loop works if I run the program but there is 1 compiling error and the second loop for the armortization does not work. Please help me, thanks.

//The java.io package provides for system input and output through data streams
import java.io.*;

//A subclass of the NumberFormat used to format numbers in Java programs 
import java.text.DecimalFormat;
 
public class AmandaMortgageRate
{
                  //The main function for the mortgage calculator
	public static void main(String[] args) throws IOException
	{
		//Declaring and constructing variables
		int [] iTerm = {84, 180, 360};
		double [] dInterest = {5.35, 5.5, 5.75};
		double dPayment, dRate, dAmount = 200000, dMonthlyInterest,dMonthlyPrincipal, dMonthlyBalance;
		DecimalFormat twoDigits = new DecimalFormat("$#,000.00"); //to format monthly payment.
		DecimalFormat df = new DecimalFormat("###,##0.00"); // to format the amount.
       		DecimalFormat numDf = new DecimalFormat("000"); // to format the sequence number.

			//output for month to years
			System.out.println();
			System.out.println("Welcome to Amanda's Mortgage Calculator");
			System.out.println();
			
		//Loop for the varying Mortgage Rates and Payments
		int p;
		for (p = 0; p <= 2; p ++)
		{
 
			//Calculation for the monthly mortgage payment
			//Calculations Retrieved from [url="http://www.1728.com/loanform.htm"]http://www.1728.com/loanform.htm[/url] on 8/15/08
			dRate = dInterest[p] / 1200;
			dPayment = (dAmount * dRate) / (1 - Math.pow(1 / (1 + dRate), iTerm[p]));
			dMonthlyInterest = (dAmount / 12) * (dInterest[p] / 100);
			dMonthlyPrincipal = (dPayment - dMonthlyInterest);
			dMonthlyBalance = (dAmount - dMonthlyPrincipal);
			int totalNumberOfMonths;
			int monthCounter=1;
 
				// Output for the loan information and the monthly payment
				int nbYear = iTerm[p] / 12;
				System.out.println();
				System.out.println("Your Monthly Payment for a " + nbYear +" years loan of $200,000 at " + dInterest[p]+ "% is:" + twoDigits.format (dPayment));
				System.out.println();

				for(; totalNumOfMonths>0; totalNumOfMonths--)
				{
            				
				//Output for loan information, monthly payment, and amortization schedule of payments
				System.out.println("Payment#   LoanBalance    InterestPaid");
            				System.out.println(numDf.format(monthCounter) + "          " +df.format(dMonthlyPrincipal)+ "          " +df.format(dPayment-dMonthlyPrincipal)));
				System.out.println("\n");
            				monthCounter++;
            				if(monthCounter%25==0)sleep(1000); // this will hesitate...
        			}
				
		}
	
	}

	 //function for the calculation of the interest into the loan payment
	public double MonthlyInterest()
	{
		//Declaring Variables for interest on the loan
		double dMonthlyInterest = 0.0;
		double dAmount = 0.0;
		double dInterest = 0.0;
 
			//Calculation for monthly interest
			dMonthlyInterest = (dAmount / 12) * (dInterest / 100);
 
		return dMonthlyInterest;
 
	}


                  //function for the calculation of the interest into the loan payment
	public double monthlyInterest()
	{
		//Declaring Variables for interest on the loan
		double dMonthlyInterest = 0.0;
		double dAmount = 0.0;
		double dInterest = 0.0;
 
			//Calculation for monthly interest
			dMonthlyInterest = (dAmount / 12) * (dInterest / 100);
 
		return dMonthlyInterest;
 
	}
 
                  //function of the calculation of the monthly principal for the loan payment
	public static double monthlyPrincipal()
	{
		//Declaring Variables for monthly principal
		double dMonthlyPrincipal = 0.0;
		double dPayment = 0.0;
		double dMonthlyInterest = 0.0;
 
			//Calculations for monthly principal
			dMonthlyPrincipal = (dPayment - dMonthlyInterest);
 
		return dMonthlyPrincipal;
 
	}
 
                  //function for the calculation of the monthy loan balance
	public static double monthlyBalance()
	{
		//Declaring Variables for monthly loan balance
		double dMonthlyBalance = 0.0;
		double dAmount = 0.0;
		double dMonthlyPrincipal = 0.0;
 
			//Calculations for monthly loan balance
			dMonthlyBalance = (dAmount - dMonthlyPrincipal);
 
		return dMonthlyBalance;
	}

	// method to make the program sleep (hesitate) for a given time (in seconds)..
    	public static void sleep(long milliseconds)
	{
        		try{
           		Thread.sleep(milliseconds);
        		}
		catch(InterruptedException e)
		{
            		//do nothing... for now...
        		}
 
	}
 
}


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 12 May 2008 - 04:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Program help with loops

#2 mistymoon1966   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: Java Program help with loops

Posted 12 May 2008 - 04:54 PM

Okay, I got the program to compile and run. The only problem is that the amortization schedule does not subtract the payment and adjust the interest for each payment. Please help.

/start of code/

//The java.io package provides for system input and output through data streams
import java.io.*;

//A subclass of the NumberFormat used to format numbers in Java programs
import java.text.DecimalFormat;

public class AmandaMortgageRate
{
//The main function for the mortgage calculator
public static void main(String[] args) throws IOException
{
//Declaring and constructing variables
int [] iTerm = {84, 180, 360};
double [] dInterest = {5.35, 5.5, 5.75};
double dPayment, dRate, dAmount = 200000, dMonthlyInterest,dMonthlyPrincipal, dMonthlyBalance;
DecimalFormat twoDigits = new DecimalFormat("$#,000.00"); //to format monthly payment.
DecimalFormat df = new DecimalFormat("###,##0.00"); // to format the amount.
DecimalFormat numDf = new DecimalFormat("000"); // to format the sequence number.

//output for month to years
System.out.println();
System.out.println("Welcome to Amanda's Mortgage Calculator");
System.out.println();

//Loop for the varying Mortgage Rates and Payments
int p;
for (p = 0; p <= 2; p ++)
{

//Calculation for the monthly mortgage payment
//Calculations Retrieved from http://www.1728.com/loanform.htm on 8/15/08
dRate = dInterest[p] / 1200;
dPayment = (dAmount * dRate) / (1 - Math.pow(1 / (1 + dRate), iTerm[p]));
dMonthlyInterest = (dAmount / 12) * (dInterest[p] / 100);
dMonthlyPrincipal = (dPayment - dMonthlyInterest);
dMonthlyBalance = (dAmount - dMonthlyPrincipal);
int totalNumberOfMonths;
int monthCounter=1;

// Output for the loan information and the monthly payment
int nbYear = iTerm[p] / 12;
System.out.println();
System.out.println("Your Monthly Payment for a " + nbYear +" years loan of $200,000 at " + dInterest[p]+ "% is:" + twoDigits.format (dPayment));
System.out.println();

int y;
for (y=1; y<=nbYear*12; y++)
{

//Output for loan information, monthly payment, and amortization schedule of payments
System.out.println("Payment# LoanBalance InterestPaid");
System.out.println(numDf.format(monthCounter) + " " +df.format(dMonthlyBalance-dMonthlyPrincipal)+ " " +df.format(dPayment-dMonthlyPrincipal));
System.out.println("\n");
monthCounter++;
if(monthCounter%25==0)sleep(2000); // this will hesitate...
}

}
dAmount=200000;
}

//function for the calculation of the interest into the loan payment
public double MonthlyInterest()
{
//Declaring Variables for interest on the loan
double dMonthlyInterest = 0.0;
double dAmount = 0.0;
double dInterest = 0.0;

//Calculation for monthly interest
dMonthlyInterest = (dAmount / 12) * (dInterest / 100);

return dMonthlyInterest;

}


//function for the calculation of the interest into the loan payment
public double monthlyInterest()
{
//Declaring Variables for interest on the loan
double dMonthlyInterest = 0.0;
double dAmount = 0.0;
double dInterest = 0.0;

//Calculation for monthly interest
dMonthlyInterest = (dAmount / 12) * (dInterest / 100);

return dMonthlyInterest;

}

//function of the calculation of the monthly principal for the loan payment
public static double monthlyPrincipal()
{
//Declaring Variables for monthly principal
double dMonthlyPrincipal = 0.0;
double dPayment = 0.0;
double dMonthlyInterest = 0.0;

//Calculations for monthly principal
dMonthlyPrincipal = (dPayment - dMonthlyInterest);

return dMonthlyPrincipal;

}

//function for the calculation of the monthy loan balance
public static double monthlyBalance()
{
//Declaring Variables for monthly loan balance
double dMonthlyBalance = 0.0;
double dAmount = 0.0;
double dMonthlyPrincipal = 0.0;

//Calculations for monthly loan balance
dMonthlyBalance = (dAmount - dMonthlyPrincipal);

return dMonthlyBalance;
}

// method to make the program sleep (hesitate) for a given time (in seconds)..
public static void sleep(long milliseconds)
{
try{
Thread.sleep(milliseconds);
}
catch(InterruptedException e)
{
//do nothing... for now...
}

}

}

/end of code/
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Program help with loops

Posted 12 May 2008 - 08:14 PM

View Postmistymoon1966, on 12 May, 2008 - 04:54 PM, said:

Okay, I got the program to compile and run. The only problem is that the amortization schedule does not subtract the payment and adjust the interest for each payment. Please help.


You'll have to subtract it in the loop
Printing dMonthyBalance - dMonthlyPrincipal
does not make dMonthyBalance = dMontlyBalance - dMonthlyPrincipal

I guess you'll have to do something like

dMonthyBalance = dMontlyBalance - dMonthlyPrincipal
and then print dMonthlyBalance

	int y;
	for (y=1; y<=nbYear*12; y++)
	{

//	Output for loan information, monthly payment, and amortization schedule of payments
		System.out.println("Payment# LoanBalance InterestPaid");
		System.out.println(numDf.format(monthCounter) + " " +df.format(dMonthlyBalance-dMonthlyPrincipal)+ " " +df.format(dPayment-dMonthlyPrincipal));
		System.out.println("\n");
		monthCounter++;
		if(monthCounter%25==0)sleep(200); // this will hesitate...
	}



Was This Post Helpful? 0
  • +
  • -

#4 mistymoon1966   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: Java Program help with loops

Posted 12 May 2008 - 10:04 PM

Honestly, I tried your suggestion, and it did not work. So, I am back at the original code. Class is over and as of last week I was getting a 100 in the class, so I will still pass. I just want to know how to fix the code so I am not totally lost when i start my next programming class next Tuesday. Since class is over, can someone tell me exactly how to fix the code and what code to add to the amortization loop to make the Loan Balance and Interest Paid correct throughout the amortization of each loan, thanks. Again the code follows, thanks.

/code/

/**
Programming 420 Week 5 Mortgage Calculator by Amanda Smith
*/

//The java.io package provides for system input and output through data streams
import java.io.*;

//A subclass of the NumberFormat used to format numbers in Java programs
import java.text.DecimalFormat;

public class AmandaMortgageRate
{
//The main function for the mortgage calculator
public static void main(String[] args) throws IOException
{
//Declaring and constructing variables
int [] iTerm = {84, 180, 360};
double [] dInterest = {5.35, 5.5, 5.75};
double dPayment, dRate, dAmount = 200000, dMonthlyInterest,dMonthlyPrincipal, dMonthlyBalance;
DecimalFormat twoDigits = new DecimalFormat("$#,000.00"); //to format monthly payment.
DecimalFormat df = new DecimalFormat("###,##0.00"); // to format the amount.
DecimalFormat numDf = new DecimalFormat("000"); // to format the sequence number.

//output for month to years
System.out.println();
System.out.println("Welcome to Amanda's Mortgage Calculator");
System.out.println();

//Loop for the varying Mortgage Rates and Payments
int p;
for (p = 0; p <= 2; p ++)
{

//Calculation for the monthly mortgage payment
//Calculations Retrieved from http://www.1728.com/loanform.htm on 8/15/08
dRate = dInterest[p] / 1200;
dPayment = (dAmount * dRate) / (1 - Math.pow(1 / (1 + dRate), iTerm[p]));
dMonthlyInterest = (dAmount / 12) * (dInterest[p] / 100);
dMonthlyPrincipal = (dPayment - dMonthlyInterest);
dMonthlyBalance = (dAmount - dMonthlyPrincipal);

int monthCounter=1;

// Output for the loan information and the monthly payment
int nbYear = iTerm[p] / 12;
System.out.println();
System.out.println("Your Monthly Payment for a " + nbYear +" years loan of $200,000 at " + dInterest[p]+ "% is:" + twoDigits.format (dPayment));
System.out.println();

int y;
for (y=1; y<=nbYear*12; y++)
{

//Output for loan information, monthly payment, and amortization schedule of payments
System.out.println("Payment# LoanBalance InterestPaid");
System.out.println(numDf.format(monthCounter) + " " +df.format(dMonthlyBalance-dMonthlyPrincipal)+ " " +df.format(dPayment-dMonthlyPrincipal));
System.out.println("\n");
monthCounter++;
if(monthCounter%25==0)sleep(2000); // this will hesitate...
}

}
dAmount=200000;
}

//function for the calculation of the interest into the loan payment
public double MonthlyInterest()
{
//Declaring Variables for interest on the loan
double dMonthlyInterest = 0.0;
double dAmount = 0.0;
double dInterest = 0.0;

//Calculation for monthly interest
dMonthlyInterest = (dAmount / 12) * (dInterest / 100);

return dMonthlyInterest;

}


//function for the calculation of the interest into the loan payment
public double monthlyInterest()
{
//Declaring Variables for interest on the loan
double dMonthlyInterest = 0.0;
double dAmount = 0.0;
double dInterest = 0.0;

//Calculation for monthly interest
dMonthlyInterest = (dAmount / 12) * (dInterest / 100);

return dMonthlyInterest;

}

//function of the calculation of the monthly principal for the loan payment
public static double monthlyPrincipal()
{
//Declaring Variables for monthly principal
double dMonthlyPrincipal = 0.0;
double dPayment = 0.0;
double dMonthlyInterest = 0.0;

//Calculations for monthly principal
dMonthlyPrincipal = (dPayment - dMonthlyInterest);

return dMonthlyPrincipal;

}

//function for the calculation of the monthy loan balance
public static double monthlyBalance()
{
//Declaring Variables for monthly loan balance
double dMonthlyBalance = 0.0;
double dAmount = 0.0;
double dMonthlyPrincipal = 0.0;

//Calculations for monthly loan balance
dMonthlyBalance = (dAmount - dMonthlyPrincipal);

return dMonthlyBalance;
}

// method to make the program sleep (hesitate) for a given time (in seconds)..
public static void sleep(long milliseconds)
{
try{
Thread.sleep(milliseconds);
}
catch(InterruptedException e)
{
//do nothing... for now...
}

}

}


//code//
Was This Post Helpful? 0
  • +
  • -

#5 BeaverDono   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-April 07

Re: Java Program help with loops

Posted 12 May 2008 - 10:20 PM

Here is a question...

for(; totalNumOfMonths>0; totalNumOfMonths--)  
{  
			   
//Output for loan information, monthly payment, and amortization schedule of payments  
System.out.println("Payment#   LoanBalance	InterestPaid");  
System.out.println(numDf.format(monthCounter) + "		  " +df.format(dMonthlyPrincipal)+ "		  " +df.format(dPayment-dMonthlyPrincipal)));  
System.out.println("\n");  
monthCounter++;  
if(monthCounter%25==0)sleep(1000); // this will hesitate...  
}  



Shouldn't totalNumOfMonths be totalNumberOfMonths? (Just look a few lines above in the original code, it spelled out fully as totalNumberOfMonths.. could this be the issue? I haven't tried compiling it because I am lazy! :D)

This post has been edited by BeaverDono: 12 May 2008 - 10:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1