What am I doing wrong?

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();
          
    }
}

Open in new window

LVL 1
flo_the_lil_minxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
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
Kevin CrossChief Technology OfficerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
contactkarthiCommented:
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();
 
		}
	}
}

Open in new window

0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Kevin CrossChief Technology OfficerCommented:
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--)

Open in new window

0
flo_the_lil_minxAuthor Commented:
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
contactkarthiCommented:
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
Kevin CrossChief Technology OfficerCommented:
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
flo_the_lil_minxAuthor Commented:
Group hug time :)

BTW Flo = she
0
Kevin CrossChief Technology OfficerCommented:
LOL, that makes perfect sense -- made the mistake of looking at the avatar being male. :) Forgot that was the default.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.