# Understanding the use of for loops as counting loops in homework review [closed]

Newbie here, first time programmer. I have a homework assignment that I am working on. Instructions:

Write an application that allows input of an integer n between 1 and 71, and (using a for or while loop, or loops) outputs 1 asterisk on the first line of output, 2 asterisks on the second line of output, etc., through n asterisks on the nth line of output. Allow the value of n to be 1 but not 71. HINT: Use a nested loop structure.

My Code:

``````import java.util.Scanner;

public class Lab6a_JamesVirden
{
public static void main(String args[])
{
int n;
int sum;
int cnt;

sum = 0;
cnt = 0;

Scanner input = new Scanner(System.in);

System.out.println();
System.out.println();
System.out.print("Input the total number of lines to output as anyinterger greater than 0 and less than 71:  ");
n = input.nextInt();

for(i = 0: i < 71; i++)
{
if ((n > 0) && (n < 71))
{
System.out.println("*" + cnt++);

}
else
{
System.out.println("The number you entered is out of range.");
System.out.print("Input the total number of lines to output as any interger greater than 1 and less than 71:  ");
n = input.nextInt();
}

}

System.out.println("Press any key to continue...");
}
}
``````

According to the assignment instructions, my output should display the * on line one, ** on the next line (line 2), * (line 3), etc, n = input.nextInt() is reached. Example: if n=5, output should be 5 lines beginning with one * on the first, two * on the second, etc until the output has reached 5 lines with five * on the fifth line. I am certain I am getting close, however my output which is printed isn't print multiple * on each line. Instead it is printing n*.

Any help is much appreciated. Thank you for your time.

-
It seems like you'r confusing loops (such as `for` loop, `while`, `do/while` loop) for statements (such as `if` statement). :) –  HelpNeeder Sep 21 '12 at 11:10

## closed as off topic by Brian Reichle, Corbin, Paul, Glenn Rogers, Quentin PradetSep 24 '12 at 16:50

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

yep. you are close.

Remember that `print` will just print the character with no carriage return but that `println` will print the character and then go onto a new line.

You need to do a little bit of work around this code block

``````if ((n > 0) && (n < 71))
{
System.out.println("*" + cnt++);
}
``````

Not every `*` needs a New Line printed after it, only that last `*` that belongs on that line.

There are some other small issues in there around the way you're using your for loops & if(checks) but that should at least get you in the direction of a working solution for now.

-

Even if it is not in the question :

``````    System.out.println();
System.out.println();
System.out.print("In......)
``````

can be write:

``````    System.out.print("\n\nIn......)
``````

and you can initialize local variable directly (EDIT after comment)

``````    int sum = 0;
int cnt = 0;
``````

and suppress

``````    sum = 0;
cnt = 0;
``````

so you can remove last lines

Read Effective Java to know how write good code

-
Local variables are not initialized automatically in Java. –  Sulthan Sep 21 '12 at 10:20
@Sulthan Exact - thanks to point out this error –  cl-r Sep 21 '12 at 12:09
I did not know java and good code can go together :D –  Aleksandar Sep 21 '12 at 17:44