IronmxidenJavaVirgin IronmxidenJavaVirgin - 1 year ago 47
Java Question

Very basic java program doesnt run

So I am a java newb, started last week and have been reading a book. In the book at the end of a chapter it gives a challenge to make a program that will output


I am trying to get it so that it outputs is the number of lines as args (That is correct right?). The issue i am having is it the program will only output a page of asterisks as in:


The code is

public class Rektifier {

public static void main(String[] args) {

int Lines = 1; // amount of lines i want

int stars = 0; //asterisk i want

int X = 0; //Counter for the asterisks for loop

while (Lines <= Integer.parseInt(args[0])){ //I think I found this on stack overflow, it is to search through the arguments for integers
Lines =+ 1; // increase by 1 for the while loop

stars =+ 1; // do this to increase by 1 for the amount of stars i want, could be any number

for (X = 0; X >= stars; X =+ 1){ // forloop, if x is less than stars it should add * to the end of the line. Then increase x by 1 to add another star until x is equal to star then it should continue
System.out.println(); //creates a new line to add on too

Answer Source

You have two loops. The outer while() loop uses Lines as its control variable. This loop decides how many stars are to be printed in each line – therefore Lines should take the values: 1, then 2, then 3, etc... (as for now it does not due to a small typo: so you should replace Lines=+1 with Lines+=1)

Your inner for loop uses X as its control variable. This loop actually prints the number of stars dictated by the Lines variable, so you should correct it to the form:
for (X = 0; X < Lines; X++)

Your variable stars is not necessary in such case.
(btw. refrain from starting variable names with capital letters; this works perfectly but is against tradition).