Solved

# What am I doing wrong?

Posted on 2008-11-05
200 Views
Hi there,

My task is to create a program using nested loops to create a display of stars on the screen that look like this...

*
**
***
****
*****

I can't see what is wrong with my code, there's probably something really silly missing.  Please could someone point me in the right direction?

TIA Flo

``````//Backward Triangle

import java.util.*;

public class BackwardTriangle

{

public static void main (String[] args);

//outer loop to count rows

for (int row = 1; row <=5; row++)

{

//first inner loop to count spaces

for (int space = 0; space < row -1; space--)

{

System.out.print(" ");

}

//second inner loop to count stars

for (int star =1; star <=5 -row; star++)

{

System.out.print("*");

}

System.out.println();

}

}
``````
0
Question by:flo_the_lil_minx

LVL 59

Expert Comment

Firstly, you are missing the opening { and closing } for your main method.  You have a semi-colon (;) instead -- so you need to remove that.

public static void main(String[] args) {
// ... rest of code ...
}
0

LVL 59

Accepted Solution

As far as the logic goes -- here are some hints/direction points:

// this says start at 0 and loop until 0 < 0 (row == 1 on first attempt)
// this goes no where OR worst could end up in an infinite loop as you are subtracting 1 from 0 each time.
for (int space = 0; space < row -1; space--)

// starting at row == 1 you will get 5 -1 stars
// check logic on number of stars you want to display by row
for (int star =1; star <=5 -row; star++)

Hopefully that helps.

Give it a try and post back your attempt and will answer questions if you have any.
0

LVL 17

Assisted Solution

for (int space = 0; space < row -1; space--)

the above code will lead you to an infinate loop...

should be space++

the problem with your code is with the loops you use to print stars and spaces..

i just changed it as below

let me know if you have any questions
``````import java.util.*;

public class BackwardTriangle

{

public static void main (String[] args)

{

// outer loop to count rows

for (int row = 1; row <=5; row++)

{

// first inner loop to count spaces

for (int space = 0; space < 5 -row; space++)

{

System.out.print(" ");

}

// second inner loop to count stars

for (int star =1; star <=row; star++)

{

System.out.print("*");

}

System.out.println();

}

}

}
``````
0

LVL 59

Expert Comment

contactkarthi,

I intentionally didn't post the full source solution as the phrasing of the question suggested this was homework.

Anyway, since you have already posted code, your statement that space-- won't work is not entirely correct.  I had this coded when I first responded and works just fine.
``````for (int space = 5 - row; space > 0; space--)
``````
0

LVL 1

Author Closing Comment

Thank you both for your answers.  It is true that it is a homework task so mwvisa1 did the right thing in not putting the code up initialy.  But, to be honest, I had grasped the concept of loops etc (you start with a value, you test it against a condition, if it meets the condition continue the loop, if not exit the loop) but after the day I've had I was grateful for the code.  It made it easier for me to compare it with mine so I could see where I was going wrong so both approaches were spot on!  Thanks again and thanks for the speedy replies
0

LVL 17

Expert Comment

mwvisa1 i normally dont post the complete code when somebody asks can some one give me a solution for this problem. he almost had this thing written except for the 2 for statements. for the first statement i posted the clarification and for the second it is just the value part that had to be changed so i tweeked it a bit.. instead of just putting the 2 lines i just put the block so that the asker could see the difference.. sorry if  i am wrong..
0

LVL 59

Expert Comment

No need to be sorry.  I was just passing along the guidance others have given me.  You are fine.  We are all just helping.  I am no pro at this...still learning the EE way myself.

:)

0

LVL 1

Author Comment

Group hug time :)

BTW Flo = she
0

LVL 59

Expert Comment

LOL, that makes perfect sense -- made the mistake of looking at the avatar being male. :) Forgot that was the default.
0

## Featured Post

### Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input: