# How do I do this, code supplied

• September 9th, 2013, 06:53 AM
MooseManX
How do I do this, code supplied
Hey I am doing a tutorial at uni and I need to write a code that

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("*");
}
}
}
}
• September 9th, 2013, 07:24 AM
jps
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
• September 9th, 2013, 07:27 AM
MooseManX
Re: How do I do this, code supplied
Code Java:

```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
• September 9th, 2013, 07:41 AM
PhHein
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.
• September 9th, 2013, 07:53 AM
MooseManX
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.
• September 9th, 2013, 08:08 AM
PhHein
Re: How do I do this, code supplied
• September 9th, 2013, 08:15 AM
MooseManX
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
• September 9th, 2013, 08:20 AM
PhHein
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.
• September 9th, 2013, 08:23 AM
MooseManX
Re: How do I do this, code supplied
Code Java:

```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("*"); } } } }```
• September 9th, 2013, 08:24 AM
jps
Re: How do I do this, code supplied
Quote:

Originally Posted by MooseManX
but the inner loop still isn't working

...and describe what isn't working means.
• September 9th, 2013, 08:25 AM
MooseManX
Re: How do I do this, code supplied
it is posting an extra line e.g

*
**
***
****
*****
*****
that's my output now
• September 9th, 2013, 08:27 AM
PhHein
Re: How do I do this, code supplied
Quote:

You have to seperate the line breaks from the output of the *.
• September 9th, 2013, 08:33 AM
MooseManX
Re: How do I do this, code supplied
Code Java:

```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
*
**
***
****
*****
• September 9th, 2013, 08:35 AM
jps
Re: How do I do this, code supplied
Quote:

Originally Posted by MooseManX
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
• September 9th, 2013, 08:35 AM
PhHein
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 :D
• September 9th, 2013, 08:46 AM
MooseManX
Re: How do I do this, code supplied
Code Java:

```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
• September 9th, 2013, 08:50 AM
jps
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
• September 9th, 2013, 09:33 AM
MooseManX
Re: How do I do this, code supplied
Code Java:

```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
• September 9th, 2013, 09:45 AM
jps
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?
• September 9th, 2013, 11:00 PM
Junky
Re: How do I do this, code supplied
For each row
print c stars
move to a new line

Not that hard really.