Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Thammer

Thread: Print a with a straight vertical line to the left instead of the right.

  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Print a with a straight vertical line to the left instead of the right.

    How can I make this program print so the output looks like this ( ignore the - -- lines ):

    --------1
    -------21
    -----321
    ---4321
    -54321

    Instead of:
    1
    21
    321
    4321
    54321

    Here is my code below.
    Java Code:
    public class c5e18c {
    public static void main(String[]args){
    	
    	int i;
    	int j;
    	
    	for(i = 1; i<=6; i++){
    		
    		for (j = i; j>=1 ;j--){
    			System.out.printf(j + " ");
    		}
    		System.out.println();
    	}	
    	
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Print a with a straight vertical line to the left instead of the right.

    Yes, you can. What have you tried?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    ababa is offline Member
    Join Date
    Oct 2014
    Location
    Bhagalpur, India
    Posts
    3
    Rep Power
    0

    Default Re: Print a with a straight vertical line to the left instead of the right.

    its not that difficult. try to modify the inner loop.

  4. #4
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Print a with a straight vertical line to the left instead of the right.

    Hi guys, thanks for a the replies.

    Could a solution could be to with the firstprint 5 spaces, then 4 spaces for the second print and so on..
    Not sure how to to do that.
    -----1 (5 spaces)
    ----21(4 spaces)
    Is this correct thinking?


    Have also tried multiple variations of formatf(), but I have not been able to figure out a way to solve it.
    Could I solve this problem by the use of formatf()?

    ababa.
    How should I modify the innerloop to make it work?

    Thanks again for the help!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Print a with a straight vertical line to the left instead of the right.

    Decompose your problem and define smaller problems: define a small method that can print i space characters on a line and define another small method that can print the numbers n, n-1, ... 1 on a line; on line j (j in the interval [1,5]), you want to print 6-j spaces and the numbers j, j-1, ... 1; so:

    Java Code:
    for (int j= 1; j <= 5; j++) {
       printSpaces(6-j);
       printNumbers(j);
       System.out.println();
    }
    Now you define those two small methods ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Print a with a straight vertical line to the left instead of the right.

    You don't need to verify it - you SEE it is correct and you already verified it before you even came here. Each minus represents a space and you can clearly see that it goes from 5 spaces to 4 spaces to 3 spaces etc. etc. By drawing it out you make it visual and undeniable - yet you're still questioning it. Why do you question what you can clearly see?

    Try thinking about it again, this time knowing for sure that you've got a correctly working brain. Toss your existing code, it will only distract you. Write out in English what you need to do.

    - I need to write a program which will print X lines.
    - On line 1 of 6 it will print 5 spaces and the number 1
    - On line 2 of 6 it will print 4 spaces and the numbers 2 1
    - On line 3 of 6 it will print 3 spaces and the numbers 3 2 1
    - On line 4 of 6 it will print 2 spaces and the numbers 4 3 2 1
    - On line 5 of 6 it will print 1 space and the numbers 5 4 3 2 1
    - On line 6 of 6 it will print 0 spaces and the numbers 6 5 4 3 2 1

    You didn't mention the sixth line but the program just seems incomplete to me without it. If you read that plain English, I'm sure you can identify some correlations that will help you to write the code. I cheated a little here by very carefully formulating it such that the truth is in plain sight ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Print a with a straight vertical line to the left instead of the right.

    Quote Originally Posted by JosAH View Post
    Decompose your problem and define smaller problems: define a small method that can print i space characters on a line and define another small method that can print the numbers n, n-1, ... 1 on a line; on line j (j in the interval [1,5]), you want to print 6-j spaces and the numbers j, j-1, ... 1; so:

    Java Code:
    for (int j= 1; j <= 5; j++) {
       printSpaces(6-j);
       printNumbers(j);
       System.out.println();
    }
    Now you define those two small methods ...

    kind regards,

    Jos
    Thank you very much for your reply.
    I created an aditional for loop for pritning spaces, the loop prints 6-i number of spaces.
    It gave me the output format I wanted. Is this a good way of doing it?


    Java Code:
    public class c5e18c {
    public static void main(String[]args){
    	
    	int i;
    	int j;
    	int k;
    	
    	for(i = 1; i<=6; i++){
    		
    		for( k= 6; k>=i ; k--){
    			System.out.print("  ");
    		}	
    		
    		for (j = i; j>=1 ;j--){
    			System.out.print(" " + j);	
    		}
    		System.out.println();
    		
    	}	
    	
    }
    }
    Last edited by Thammer; 10-28-2014 at 12:37 PM.

  8. #8
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Print a with a straight vertical line to the left instead of the right.

    Quote Originally Posted by gimbal2 View Post
    You don't need to verify it - you SEE it is correct and you already verified it before you even came here. Each minus represents a space and you can clearly see that it goes from 5 spaces to 4 spaces to 3 spaces etc. etc. By drawing it out you make it visual and undeniable - yet you're still questioning it. Why do you question what you can clearly see?

    Try thinking about it again, this time knowing for sure that you've got a correctly working brain. Toss your existing code, it will only distract you. Write out in English what you need to do.

    - I need to write a program which will print X lines.
    - On line 1 of 6 it will print 5 spaces and the number 1
    - On line 2 of 6 it will print 4 spaces and the numbers 2 1
    - On line 3 of 6 it will print 3 spaces and the numbers 3 2 1
    - On line 4 of 6 it will print 2 spaces and the numbers 4 3 2 1
    - On line 5 of 6 it will print 1 space and the numbers 5 4 3 2 1
    - On line 6 of 6 it will print 0 spaces and the numbers 6 5 4 3 2 1

    You didn't mention the sixth line but the program just seems incomplete to me without it. If you read that plain English, I'm sure you can identify some correlations that will help you to write the code. I cheated a little here by very carefully formulating it such that the truth is in plain sight ;)
    Thank you for your reply, yes you are correct!
    The problem for me was to code how to get the program to print the amount of spaces I wanted per line, but I got it to work now :)
    Thanks!

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Print a with a straight vertical line to the left instead of the right.

    Any way that works is a "good" way.

    Question is: do you think it is good enough? I see three loops. I wonder if you can do it with only one with those correlations I mentioned earlier. Some things you need to "count" with a loop, some things you can simply derive from information you already have. One piece of information you have is that the length of a line is 6 characters. For each line, you know the line number which you can number from 1 to 6. With those two pieces of information, you know basically everything there is to know. For example, numberOfSpaces = lineLength - lineNumber.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Print a with a straight vertical line to the left instead of the right.

    Quote Originally Posted by gimbal2 View Post
    Any way that works is a "good" way.

    Question is: do you think it is good enough? I see three loops. I wonder if you can do it with only one with those correlations I mentioned earlier. Some things you need to "count" with a loop, some things you can simply derive from information you already have. One piece of information you have is that the length of a line is 6 characters. For each line, you know the line number which you can number from 1 to 6. With those two pieces of information, you know basically everything there is to know. For example, numberOfSpaces = lineLength - lineNumber.
    Interesting point.
    I will give it a try and see if I can reduce the amount of loops used :)
    gimbal2 likes this.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Print a with a straight vertical line to the left instead of the right.

    You can do also do it using a string with a simple System.out.printf() statement using an appropriate width specifier. Check out the Formatter class for more info.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Print Left Diagonal
    By BlueBird in forum New To Java
    Replies: 5
    Last Post: 02-07-2012, 02:40 PM
  2. Where does he print this certain line?
    By Lund01 in forum Advanced Java
    Replies: 6
    Last Post: 11-18-2010, 03:23 PM
  3. print every other line
    By welikedogs in forum New To Java
    Replies: 23
    Last Post: 11-16-2010, 06:40 PM
  4. Print to next Line
    By singularity in forum New To Java
    Replies: 13
    Last Post: 09-10-2009, 10:03 AM
  5. JButtons in a straight line on the BOTTOM
    By paul in forum AWT / Swing
    Replies: 0
    Last Post: 07-16-2007, 05:47 PM

Posting Permissions

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