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

Thread: Problem with Output # of lines

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Output # of lines

    Hi all,

    I am having a problem with my code,
    It is supposed to display 10 lines of text when I run it, but at first it does that, then it goes and displays 20 lines at a time. I am at my wits end with this one.

    Can anybody help??
    Here is my code
    /*Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:
     
    - 7 year at 5.35%
    - 15 year at 5.5%
    - 30 year at 5.75%
     
    Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.*/
    import java.text.*;
    import java.lang.*;
    import java.io.*;
    public class Mortgage
    {
    	public static void main(String[] args)
    	{
    		double rate[] = new double[] {.0535, .055, .0575};
    		double loan = 200000;
    		int term[] = new int[] {7, 15, 30};
    		double payment;
    		double balance;
    		double interest;
    		double principal;
    		int month;
    		int i = 0;
    		int k = 0;
    		int l = 0;
    		/*Declaration of decimal formatting*/
    		DecimalFormat Currency = new DecimalFormat ("0.00");
    		BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
    		/*Monthly Mortgage Calculator*/
    		try
    		{
    			System.out.println("Firstly, we will display the payment, balance and interest for a ");
    			System.out.println("\n$"+Currency.format(loan)+" over "+term[0]+" years at %"+(rate[0]*100));
    			System.out.println("\nMonth\t\tPayment\t\t        Balance\t\t        Interest\n");
    			principal = loan;
    			while(i<term[0]*12)
    			{
    				for(int j = 0; j<10; j++)
    				{
    					payment = loan*((rate[0]/12)*Math.pow((1+(rate[0]/12)),(term[0]*12)))/(Math.pow((1+(rate[0]/12)),(term[0]*12))-1);
    					month = i+1;
    					interest = principal*rate[0]/12;
    					balance = principal + interest - payment;
    					System.out.println(month+"\t\t$"+Currency.format(payment)+"\t\t$"+Currency.format(balance)+"\t\t$"+Currency.format(interest));
    					principal = balance;
    					i++;
    					if (i==term[0]*12)
    						break;
    				}
            	buffer.read();
    			}
    			System.out.println("Press the ENTER key to view next payments...\n");
    		}
    		catch(IOException e)
    		{
    		System.out.println("Error: "+e);
    		}
    		try
    		{
    			System.out.println("Next, we will display the payment, balance and interest for a ");
    			System.out.println("\n$"+Currency.format(loan)+" over "+term[1]+" years at %"+(rate[1]*100));
    			System.out.println("\nMonth\t\tPayment\t\t        Balance\t\t        Interest\n");
    			principal = loan;
    			while(k<term[1]*12)
    			{
    				for(int j = 0; j<10; j++)
    				{
    					payment = loan*((rate[1]/12)*Math.pow((1+(rate[1]/12)),(term[1]*12)))/(Math.pow((1+(rate[1]/12)),(term[1]*12))-1);
    					month = k+1;
    					interest = principal*rate[1]/12;
    					balance = principal + interest - payment;
    					System.out.println(month+"\t\t$"+Currency.format(payment)+"\t\t$"+Currency.format(balance)+"\t\t$"+Currency.format(interest));
    					principal = balance;
    					k++;
    					if (k==term[1]*12)
    						break;
    				}
            	buffer.read();
    			}
    			System.out.println("Press the ENTER key to view next payments...\n");
    		}
    		catch(IOException e)
    		{
    		System.out.println("Error: "+e);
    		}
    		try
    		{
    			System.out.println("Lastly, we will display the payment, balance and interest for a ");
    			System.out.println("\n$"+Currency.format(loan)+" over "+term[2]+" years at %"+(rate[2]*100));
    			System.out.println("\nMonth\t\tPayment\t\t        Balance\t\t        Interest\n");
    			principal = loan;
    			while(l<term[2]*12)
    			{
    				for(int j = 0; j<10; j++)
    				{
    					payment = loan*((rate[2]/12)*Math.pow((1+(rate[2]/12)),(term[2]*12)))/(Math.pow((1+(rate[2]/12)),(term[2]*12))-1);
    					month = l+1;
    					interest = principal*rate[2]/12;
    					balance = principal + interest - payment;
    					System.out.println(month+"\t\t$"+Currency.format(payment)+"\t\t$"+Currency.format(balance)+"\t\t$"+Currency.format(interest));
    					principal = balance;
    					l++;
    					if (l==term[2]*12)
    						break;
    				}
            	buffer.read();
    			}
    		}
    		catch(IOException e)
    		{
    		System.out.println("Error: "+e);
    		}
    	}
    }

    Thanks in advance.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Output # of lines

    Thanks everyone for all the help!!

Similar Threads

  1. problem with output
    By Timur in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 11th, 2011, 09:56 AM
  2. JTextArea output problem
    By grimx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 8th, 2010, 08:54 PM
  3. Output problem (newbie)
    By Asido in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2010, 01:19 PM
  4. xml output problem
    By tsili in forum Java Theory & Questions
    Replies: 4
    Last Post: May 25th, 2010, 05:56 AM
  5. Display output from a file using regular expression
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 29th, 2008, 10:33 AM