Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Why is this nested for loop not printing 5 spaces on the top line?

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Why is this nested for loop not printing 5 spaces on the top line?

    My question is the comment in the following code. Thanks in advance.

    for( i=0; i<num; i++){
     
         for(j=num-i; j>1; j-- ){         // first pass of this loop should print 5 spaces? Because input (5) minus i (0) should be 5? Also if someone could explain to me why j>1. 
         System.out.print(" ");
         }
     
         for(j=0; j<=i; j++){
         System.out.print("* ");
        }
    }
     
    Console
     
    Enter height of pyramid.
    5
        *                          
       * *                        
      * * * 
     * * * * 
    * * * * *
    Last edited by FightingIrishman; December 29th, 2021 at 05:20 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why is this nested for loop not printing 5 spaces on the top line?

    Try debugging the code by having it print the value of the loop index instead of a space.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    FightingIrishman (December 31st, 2021)

  4. #3
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Why is this nested for loop not printing 5 spaces on the top line?

    If j starts at 5 and the loop condition is j>1 you'll get only 4 iterations (j = 5, 4, 3, 2)
    The condition should be j>0 (or j>=1 if you prefer).

  5. The Following User Says Thank You to cbreemer For This Useful Post:

    FightingIrishman (December 31st, 2021)

  6. #4
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Why is this nested for loop not printing 5 spaces on the top line?

    Thanks mate. You're right, it was the "j>1" that was doing it.

    The code worked fine but I'm learning from example code so I was wondering what was making the spaces print one less than the input number.

  7. #5
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    117
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Why is this nested for loop not printing 5 spaces on the top line?

    The nested loop looks good. But why would it print * on the second loop?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why is this nested for loop not printing 5 spaces on the top line?

    why would it print * on the second loop?
    Try debugging the code by adding some print statements that show the values of the variables as the code executes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. nested loop
    By karthik.m in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2014, 11:09 AM
  2. Spaces at the start of every other line in a for loop
    By Dingles in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 22nd, 2013, 10:33 PM
  3. reverse line feed using java printing
    By ranjith. in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2013, 09:17 AM
  4. Nested For Loop!
    By samadniz in forum Object Oriented Programming
    Replies: 3
    Last Post: September 3rd, 2012, 04:03 PM
  5. printing in java; missing spaces
    By user85116 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 02:51 PM