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

Thread: How do I do this, code supplied

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I do this, code supplied

    Hey I am doing a tutorial at uni and I need to write a code that

    Task 3
    Write a program that accept from the command line number n and then print stars ‘*’ in n rows.
    For example, if n = 5 then the program should print:
    *
    * *
    * * *
    * * * *
    * * * * *
    Note that you must let the user input the number for rows from the command line. For example, if the command line input is 5, you will have args[0] = "5". Then you can use
    int n = Integer.parseInt(args[0]); // now n=5

    here is my code so far, I have asked my tutorial teacher. All she did was pass me a pen and paper and smile and there is nothing in the lectures about how to do it. Any help, as quick as possible would be appreciated.

    public class ArgsLoop {
    public static void main(String[] args) {
    int n = Integer.parseInt(args[0]); // now n=5
    for(int i=0;r<n;i++){
    System.out.println("*");
    for(int c=i; c<n; c++){
    System.out.print("*");
    }
    }
    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I do this, code supplied

    for(int i = 0; r < n; i++) {
    Where does the 'r' come from?

    Please use code tags when posting code, help can be found here

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this, code supplied

    public class ArgsLoop {
    	public static void main(String[] args) {
    	int n = Integer.parseInt(args[0]); // now n=5
    		for(int i=1;i<n;i++){
    			System.out.println("*");
    			for(int c=0; c<i; c++){
    				System.out.print("*");
    			}
    			}
    		}
    	}

    i is suppose to be the rows, c is the columns I've been working on it a bit, it comes up with the result

    *
    **
    ***
    ****
    ****
    now

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How do I do this, code supplied

    You have to seperate the line breaks from the output of the *.
    The two nested loops are a good start. Do it again with paper and pencil and find out how often each loop should run.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this, code supplied

    That's not helping ^. Can you show me how to do it in code, don't you think I have used paper and pen? That's exactly what I wrote in my first post.
    I have been playing around with this code for hours, I know it's something simple. But that is really not helping it at all. I already have the second loop to set up to not write lines.

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How do I do this, code supplied

    Then your pencil is broken! Your outer loop only runs four times. I will not provide any code, that's your task. I'll only give, in your perception, unhelpful advice.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this, code supplied

    for(int i=0;i<n;i++){
    System.out.println("*");

    I fixed the outer loop,
    it runs five times now, but the inner loop still isn't working

  8. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How do I do this, code supplied

    As I cannot see your inner loop, it's hard to tell. Please always post your complete code, so that members see your actual version.

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this, code supplied

    public class ArgsLoop {
    	public static void main(String[] args) {
    	int n = Integer.parseInt(args[0]); // now n=5
    		for(int i=0;i<n;i++){
    			System.out.println("*");
    			for(int c=i; c>-1; c--){
    				System.out.print("*");
    			}
    		}
    	}
    }

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I do this, code supplied

    Quote Originally Posted by MooseManX View Post
    but the inner loop still isn't working
    ...and describe what isn't working means.

  11. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this, code supplied

    it is posting an extra line e.g

    *
    **
    ***
    ****
    *****
    *****
    that's my output now

  12. #12
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How do I do this, code supplied

    From my earlier reply
    You have to seperate the line breaks from the output of the *.

  13. #13
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this, code supplied

    public class ArgsLoop {
    	public static void main(String[] args) {
    	int n = Integer.parseInt(args[0]); // now n=5
    		for(int i=0;i<n;i++){
    			System.out.println();
    			System.out.print("*");
    			for(int c=i-1; c>-1; c--){
    				System.out.print("*");
    			}
    		}
    	}
    }

    Okay that just clicked what you meant, played around with my code some more
    and I have come up with this output which is correct except a space is placed here. I'm not sure if this will matter or not could you explain why it is there

    < there is a space here
    *
    **
    ***
    ****
    *****

  14. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I do this, code supplied

    Quote Originally Posted by MooseManX View Post
    could you explain why it is there

    < there is a space here
    *
    **
    ***
    ****
    *****
    System.out.println();
    is the first thing inside the loop, so it is the first thing that happens
    Put the System.out.println(); somewhere else.
    If you worked this out on paper, step by step, it would have been clear how to make it work before you wrote public class ArgsLoop

  15. #15
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How do I do this, code supplied

    It's there, because the first thing you do in your outer loop is printing a new line. I bet when you used the pencil, the first thing was drawing a *, not moving to a new line

  16. #16
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this, code supplied

    public class ArgsLoop {
    	public static void main(String[] args) {
    	int n = Integer.parseInt(args[0]); // now n=5
    		for(int i=0;i<n;i++){
    			System.out.print("*");
    			System.out.println();
    			for(int c=i; c>-1; c--){
    				System.out.print("*");
    			}
    		}
    	}
    }
    If I separate them like this my output just becomes.

    *
    **
    ***
    ****
    *****
    *****
    again

  17. #17
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I do this, code supplied

    Back to the drawing board. (or pencil and paper)
    Hint, you do not need two lines that say System.out.print("*");
    Come up with how to do it first, then translate that solution to code

  18. #18
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I do this, code supplied

    public class ArgsLoop {
    	public static void main(String[] args) {
    	int n = Integer.parseInt(args[0]); // now n=5
    		for(int i=0;i<n;i++){
    			System.out.println();
    			for(int c=i; c>-1; c--){
    				System.out.print("*");
    			}
    		}
    	}
    }

    So I realized I do not need the outer * print. However this is still leaving me with a new line before the result

  19. #19
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I do this, code supplied

    When you did it on paper, (because we know you did this first, because it makes writing the code easier), did you move to a new line and then write *; or did you write * and then move to a new line?
    What does the code do? Write * or go to a new line first?

  20. #20
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How do I do this, code supplied

    For each row
    print c stars
    move to a new line

    Not that hard really.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM