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 7 of 7

Thread: need help printing hourglass shape

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help printing hourglass shape

    I am only able to print upper half of the hourglass. Please help with the ower half.
    public static void printHourglass(int size, char symbol)
        {   
            int count = 0;
            int count2 = 0;
            boolean lower = false;
            for(int lines = 0; lines < size; lines++)
            {   
                //upper half
     
                for(int spaces = 0; spaces < lines; spaces++)
                {
                    if(lower == false)
                        System.out.print(" ");
                    else
                        break;
     
                }
                count = 0;
                for(int j = lines; j <= size - (lines + 1); j++)
                {
                   count++;
                   System.out.print(symbol);
     
                }
     
     
                for(int spaces = 0; spaces < lines;spaces++)
                {
                    if(lower == false)
                        System.out.print(" ");
                    else
                        break;
     
     
                }
                System.out.println("");
     
     
     
            }  
     
     
     
        }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,324
    My Mood
    Hungover
    Thanks
    142
    Thanked 624 Times in 535 Posts

    Default Re: need help printing hourglass shape

    We can't run this code because it isn't in the form of an MCVE.

    What have you tried? What exactly are you confused about? We aren't just going to do your homework for you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,034
    My Mood
    Mellow
    Thanks
    184
    Thanked 646 Times in 633 Posts

    Default Re: need help printing hourglass shape

    If you've successfully coded the top half, why can't you reverse the logic to print the lower half? Similar to:

    A
    B
    C
    C
    B
    A

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help printing hourglass shape

    This is not homework. I am trying to teach myself java.

    This code is supposed to print symbols in hourglass shape.
    size must be an odd number. For example giving it a parameter of 3 and 'Q' should print

    QQQ
    Q
    QQQ

    I'm able to print the first two lines but not the third.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: need help printing hourglass shape

    but not the third.
    Can you explain what the problem is printing the third line? For that simple example the third line is the same as the first line.
    Look at the post#3. Think about how the line lengths shorten and then lengthen.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help printing hourglass shape

    This is not homework. I am trying to teach myself java.

    This code is supposed to print symbols in hourglass shape.
    size must be an odd number. For example giving it a parameter of 3 and 'Q' should print


    QQQ
                  Q
                QQQ

    I'm able to print the first two lines but not the third.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: need help printing hourglass shape

    but not the third.
    Please explain what the problem is.

    For the 3 lines you've shown:
    set cnt=3
    set change to -2
    print cnt Qs
    cnt = cnt + change
    print cnt Qs
    now set change to +2 -> to add 2 to cnt instead of subtracting 2

    Put the above in a loop to print the total lines
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. printing asterisks shape in java
    By ATB in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 16th, 2014, 07:34 PM
  2. Hourglass using recursion
    By nWeid1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 1st, 2012, 10:30 PM
  3. Printing a Diamond shape Problem.
    By Sev in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 23rd, 2011, 08:05 AM
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread