Results 1 to 9 of 9
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default output format problem.

    I need my output to look like this:

    Principal 6% 6.5% 7% 7.5% 8% 8.5% 9% 9.5% 10%
    100000 600 632 665 699 733 768 804 840 877
    120000 719 758 798 839 880 922 965 1009 1053
    140000
    160000
    180000
    200000

    I'm not sure how to accomplish it. Here is what I have:

    Java Code:
    public class ProgrammingProblemTwo {
    
        public static void main(String[] args){
    
        	//declare variables
    		double r = 0, N = 30, monthlyPayment = 0;
    
    
    		double sum;
    
    
    		System.out.println("Principal 6" + "%" + "  6.5" + "%" + "  7" + "%" +
    			"  7.5" + "%" + "  8" + "%" + "  8.5" + "%" + "  9" + "%" +
    				"  9.5" + "%" + "  10" + "%\n");
    
    		//create loop to increment principal in dollars, L
        	for(int L = 100000; L <= 200000; L += 20000){
        		System.out.println(L);
        		monthlyPayment = 0;
    
        			//create loop to increment interest rate, r
        			for(r = .06; r <= .10; r += .005){
        				double temp = 1 + r / 12;
    					double temp1 = 1;
    
        				//create (1 + r / 12) exponent 12N formula
    					for (int i = 1; i<=12*N;i++){
    						temp1 = temp1*temp;
    					}
    						monthlyPayment = (L*(r/12)*temp1)/(temp1-1);
    						System.out.printf("       %.0f", monthlyPayment);
    				}
        	}
        }
    }
    I am having two problems that I see so far. The first is that the monthly payments are one line below where they need to be. The second problem is that there is too much of a gap between the monthly payments, so they wrap around.

    So I need to figure out how to move System.out.printf(" %.0f", monthlyPayment); up one line.

    I also need to figure out how to keep the gap for the first monthly payment but bring the other monthly payments closer together so that they somewhat fit underneath the percentages.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First off, don't do this:
    Java Code:
    System.out.printf("       %.0f", monthlyPayment);

    as you're not using printf to it's full formattingadvantage. Rather, do something like:

    Java Code:
    System.out.printf("%6.0f", monthlyPayment); // the 6 may need to be changed to diff number

    or


    Java Code:
    System.out.printf("%-6.0f", monthlyPayment); // again the -6 may need to be bigger

    so you would let printf set the width of the data displayed.


    Next, you do know that this:
    Java Code:
    System.out.println(L);
    will print the value of L and then a carriage return, so you're putting a carriage return at the beginning of the line. Are you sure you want to do this? Rather, consider using printf for this output as well. Just be sure to do a println at the end of the line.

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Next, you do know that this:
    Java Code:
    System.out.println(L);
    will print the value of L and then a carriage return, so you're putting a carriage return at the beginning of the line. Are you sure you want to do this? Rather, consider using printf for this output as well. Just be sure to do a println at the end of the line.
    Yes, I put in the carriage return so that L would be printed like this:

    100000
    120000
    140000
    160000
    180000
    200000

    Quote Originally Posted by Fubarable View Post
    First off, don't do this:
    Java Code:
    System.out.printf("       %.0f", monthlyPayment);

    as you're not using printf to it's full formattingadvantage. Rather, do something like:

    Java Code:
    System.out.printf("%6.0f", monthlyPayment); // the 6 may need to be changed to diff number

    or


    Java Code:
    System.out.printf("%-6.0f", monthlyPayment); // again the -6 may need to be bigger

    so you would let printf set the width of the data displayed.
    Ok, I see what you are saying I went ahead and changed it. However, the problem that I am running into is that I have to have the width 12 spaces in order to make it appear to the right of L. This gives everything a width of 12 spaces which causes it to wrap around. How do I keep the width of 12 for the first monthlyPayment value but substantially shrink the width of the rest of them?

    Here's what I have after the change:

    Java Code:
    public class ProgrammingProblemTwo {
    
        public static void main(String[] args){
    
        	//declare variables
    		double r = 0, N = 30, monthlyPayment = 0;
    		double sum;
    
    		//output
    		System.out.println("Principal 6" + "%" + "  6.5" + "%" + "  7" + "%" +
    			"  7.5" + "%" + "  8" + "%" + "  8.5" + "%" + "  9" + "%" +
    				"  9.5" + "%" + "  10" + "%\n");
    
    		//create loop to increment principal in dollars, L
        	for(int L = 100000; L <= 200000; L += 20000){
        		System.out.println(L);
        		monthlyPayment = 0;
    
        			//create loop to increment interest rate, r
        			for(r = .06; r <= .10; r += .005){
        				double temp = 1 + r / 12;
    					double temp1 = 1;
    
        				//create (1 + r / 12) exponent 12N formula
    					for (int i = 1; i<=12*N;i++){
    						temp1 = temp1*temp;
    					}
    						monthlyPayment = (L*(r/12)*temp1)/(temp1-1);
    						System.out.printf("%12.0f", monthlyPayment);
    				}
        	}
        }
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    The 6 or 12 is how wide a cell in the table will be. you you want some columns to be 12 chars wide and some to be 8 chars wide then you need to use different printf statements or use a variable.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jim01 View Post
    Yes, I put in the carriage return so that L would be printed like this:

    100000
    120000
    140000
    160000
    180000
    200000
    And you wonder why the numbers are one line below where they should be! Again, use printf for these numbers.


    Ok, I see what you are saying I went ahead and changed it. However, the problem that I am running into is that I have to have the width 12 spaces in order to make it appear to the right of L. This gives everything a width of 12 spaces which causes it to wrap around. How do I keep the width of 12 for the first monthlyPayment value but substantially shrink the width of the rest of them?
    Don't use 12 for all of the printf constants. You can use multiple constants and variables for printf, you know.

    i.e.,
    Java Code:
    System.out.printf("%12d %-6.1f %-6.1f%n", myInt, myDouble1, myDouble2);

  6. #6
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    And you wonder why the numbers are one line below where they should be! Again, use printf for these numbers.
    I will keep messing with it to see if I can figure it out.

    Don't use 12 for all of the printf constants. You can use multiple constants and variables for printf, you know.

    i.e.,
    Java Code:
    System.out.printf("%12d %-6.1f %-6.1f%n", myInt, myDouble1, myDouble2);
    First off, I would like to thank you for your help. I know it can be very trying dealing with knuckle-headed newbies like myself.

    I knew that you could use multiple constants with printf, but I've only done that with simple math programs. In this case all of my output is coming from the nested loop. I have one output constant generating six iterations of output. I don't know how to isolate each iteration and make it a separate constant. I didn't even know you could do such a thing.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Make your life easier. Create a method to calculate the interest. It has two parameters (principle, rate) and returns the interest. Then your print statement becomes
    Java Code:
    System.out.printf("%12d %-6.1f %-6.1f etc%n", principal, methodCall, methodCall, methodCall, etc);
    Actually it would be even better to use a loop and array. Then the printf statement becomes
    Java Code:
    loop {
        calculate the interest
    }
    System.out.printf("%12d %-6.1f %-6.1f etc%n", principal, array[0], array[1], etc);
    Last edited by Junky; 04-18-2011 at 03:45 AM.

  8. #8
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Make your life easier. Create a method to calculate the interest. It has two parameters (principle, rate) and returns the interest. Then your print statement becomes
    Java Code:
    System.out.printf("%12d %-6.1f %-6.1f etc%n", principal, methodCall, methodCall, methodCall, etc);
    Actually it would be even better to use a loop and array. Then the printf statement becomes
    Java Code:
    loop {
        calculate the interest
    }
    System.out.printf("%12d %-6.1f %-6.1f etc%n", principal, array[0], array[1], etc);
    We won't learn about making new methods for another 6 weeks. However, we start arrays tomorrow. I've been hearing a lot about arrays lately so I am looking forward to reading that chapter.

    Thank you for your help.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    We won't learn about making new methods for another 6 weeks. However, we start arrays tomorrow. I've been hearing a lot about arrays lately so I am looking forward to reading that chapter.
    You know, it wouldn't harm you to read further before hand. In fact it'll only make you understand things faster because when you go over it again in your class it'll be like revision.

Similar Threads

  1. Problem with a output
    By feastfulsaint in forum Advanced Java
    Replies: 3
    Last Post: 10-25-2010, 02:20 AM
  2. Format the output
    By Moustafa taha in forum New To Java
    Replies: 5
    Last Post: 10-18-2010, 05:01 AM
  3. xml output problem
    By tsili in forum XML
    Replies: 2
    Last Post: 05-24-2010, 12:27 AM
  4. output for list of names in a format
    By Ms.Ranjan in forum New To Java
    Replies: 7
    Last Post: 06-18-2009, 04:47 PM
  5. Output Problem
    By jazz2k8 in forum New To Java
    Replies: 4
    Last Post: 05-26-2008, 10:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •