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

Thread: Displaying an hourglass

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

    Default Displaying an hourglass

    Hello everybody,
    I recently started learning java and I am having a problem. The code I am writing is supposed to display an hourglass like this. The lines match up so it looks like an hourglass.
    |""""""""""|  top part
    \::::::::/
    \::::::/
    \::::/  middle part
    \::/
    ||  Vertical
    /::\
    /::::\  bottom part
    /::::::\
    /::::::::\
    |""""""""""|


    but when I run my code I get this. I don't know why I am getting this and it would be very helpful if anyone gave me some advice as to what I can do to make the bottom hourglass look like the top.
    |:::::::::
    ::::::::
    ::::::
    ::::
    ::
    ||
    ::
    ::::
    ::::::
    ::::::::
    |:::::::::



    public class HourGlass
    {
       public static void main(String[] args)
       {
          topPart();
          middlePart();
          verticalPart();
          bottomPart();
          topPart();
       }
     
     
       //This method produces the top part of the hourglass
       public static void topPart()
       {
          System.out.print("|");
          for (int i =1; i <=10; i++)
          {
             System.out.print(":");
          }
          System.out.print("|");
     
          System.out.println();
       }
     
       //This method produces the middle part of the hourglass
       public static void middlePart()
       {
           for (int line =1; line <=4; line++)
          {
             for (int space=1; space<= (line * 2 ); space++)
             {
                System.out.print(" ");
             }
     
             for (int semicolon =1; semicolon <= (line * -2 + 10); semicolon++)
             {
     
                System.out.print(":");
             }
     
              for (int space=1; space <= (line * 2); space++)
             {
                System.out.print(" ");
     
             }
     
             System.out.println();
           }
       }
     
       //This method produces the vertical line of the hourglass         
       public static void verticalPart()
       {
          for (int space = 1; space <=5; space++)
          {
            System.out.print("");
          }
          System.out.print("||");
     
          for (int space = 1; space <=5; space++)
          {
            System.out.print(" ");
          }
          System.out.println();
       }
     
       //This method produces the bottom part of the hourglass
       public static void bottomPart()
       {   
     
     
           for(int line =1; line <=4; line++)
           {
             for (int space=1; space <= (line * -2 + 10); space++)
             {
                System.out.print(" ");
             }
     
             for (int semicolon =1; semicolon <= (line * 2); semicolon++)
             {
                System.out.print(":");
             }
     
              for (int space=1; space <= (line * 2); space++)
             {
                System.out.print(" ");
             }
     
             System.out.println();
       }
    }
    }


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

    Default Re: Displaying an hourglass

    The display of the output needs to be wrapped in code tags to preserve its formatting. I assume that there are leading spaces that the forum's formatting code has removed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Displaying an hourglass

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    I ran your program, and this is what I got (in code tags as Norm suggested):
    |::::::::::|
      ::::::::  
        ::::::    
          ::::      
            ::        
    ||     
            ::  
          ::::    
        ::::::      
      ::::::::        
    |::::::::::|
    An interesting thing to note is that most every line ENDS in more spaces than are needed.

    The good news is that you got the top and bottom part right!

    Each line other than the middle is a mix of leading spaces, a '\' character, a number of colons, and a '/' character, varying in a simple pattern. You might write the pattern out in a table, something like:

     Line        Leading       Colon
                 spaces        chars
       2            1             8
       3            2             6
       4            3             4
    etc. . .
    From a table like that you might see a pattern that will help you construct the necessary for() loops.

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

    Default Re: Displaying an hourglass

    Hello everyone I fixed my code and I got this
    public class HourGlass
    {
       public static void main(String[] args)
       {
          topPart();
          middlePart();
     
          bottomPart();
          topPart();
       }
     
     
       //This method produces the top part of the hourglass
       public static void topPart()
       {
          System.out.print("|");
          for (int i =1; i <=10; i++)
          {
             System.out.print(":");
          }
          System.out.print("|");
     
          System.out.println();
       }
     
       //This method produces the middle part of the hourglass
       public static void middlePart()
       {
           for (int line =1; line <=4; line++)
          {
             for (int space=1; space <= (line * 1 + 1 ); space++)
             {
                System.out.print(" ");
             }
     
             for (int semicolon =1; semicolon <= (line * -2 + 10); semicolon++)
             {
     
                System.out.print(":");
             }
     
              for (int space=1; space <= (line * 1 + 1); space++)
             {
                System.out.print(" ");
     
             }
     
             System.out.println();
           }
       }
     
       //This method produces the vertical line of the hourglass         
     
       //This method produces the bottom part of the hourglass
       public static void bottomPart()
       {   
      for(int line =1; line <=4; line++)
           {
             for (int space=1; space <= (line * -1 + 6); space++)
             {
                System.out.print(" ");
             }
     
             for (int semicolon =1; semicolon <= (line * 2); semicolon++)
             {
                System.out.print(":");
             }
     
              for (int space=1; space <= (line * -1 + 6); space++)
             {
                System.out.print(" ");
             }
     
             System.out.println();
       }
    }
    }

    This code gives me the image at the top, but I need the image at the bottom
    |::::::::::|
      ::::::::  
       ::::::   
        ::::    
         ::     
         ::     
        ::::    
       ::::::   
      ::::::::  
    |::::::::::|
     
     
    |""""""""""|   top part
     \::::::::/
      \::::::/
       \::::/   middle part
        \::/
         ||  Vertical
        /::\
       /::::\   bottom part
      /::::::\
     /::::::::\
    |""""""""""|

    When I add the vertical part into my code
    public class HourGlass
    {
       public static void main(String[] args)
       {
          topPart();
          middlePart();
          verticalPart();
          bottomPart();
          topPart();
       }
     
     
       //This method produces the top part of the hourglass
       public static void topPart()
       {
          System.out.print("|");
          for (int i =1; i <=10; i++)
          {
             System.out.print(":");
          }
          System.out.print("|");
     
          System.out.println();
       }
     
       //This method produces the middle part of the hourglass
       public static void middlePart()
       {
           for (int line =1; line <=4; line++)
          {
             for (int space=1; space <= (line * 1 + 1 ); space++)
             {
                System.out.print(" ");
             }
     
             for (int semicolon =1; semicolon <= (line * -2 + 10); semicolon++)
             {
     
                System.out.print(":");
             }
     
              for (int space=1; space <= (line * 1 + 1); space++)
             {
                System.out.print(" ");
     
             }
     
             System.out.println();
           }
       }
     
       //This method produces the vertical line of the hourglass         
       public static void verticalPart()
       {
          for (int line = 1; line <= (line * 5); line++)
          {
            System.out.print(" ");
          }
          System.out.print("||");
     
          for (int line = 1; line <= (line * 5); line++)
          {
            System.out.print(" ");
          }
          System.out.println();
       }
     
       //This method produces the bottom part of the hourglass
       public static void bottomPart()
       {   
     
     
           for(int line =1; line <=4; line++)
           {
             for (int space=1; space <= (line * -1 + 6); space++)
             {
                System.out.print(" ");
             }
     
             for (int semicolon =1; semicolon <= (line * 2); semicolon++)
             {
                System.out.print(":");
             }
     
              for (int space=1; space <= (line * -1 + 6); space++)
             {
                System.out.print(" ");
             }
     
             System.out.println();
       }
    }
    }
    I get the hourglass without the vertical part, if anyone has any suggestions as to how I can fix this it would be greatly appreciated

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Displaying an hourglass

    Can you post the current output and explain what is wrong with it?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Displaying an hourglass

    The code you posted has an infinite loop - a loop that never stops. Your current design makes it very easy to find which 'part' results in an infinite loop, because the methods can be commented out one at a time in the main() method.

    Can you figure out which method results in an infinite loop, why it happens, and fix it?

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Displaying an hourglass

    Hello everybody,
    I was able to get rid of the infinite loop that was happening at the vertical method. Now I'm having trouble aligning the two || at the center of the hourglass. If anyone has any suggestions as to how I can fix this it would be greatly appreciated.

    The hourglass that I get is
    |""""""""""|
      ::::::::  
       ::::::   
        ::::    
         ::     
    || 
         ::     
        ::::    
       ::::::   
      ::::::::  
    |""""""""""|

    This is the code I have.

    public class HourGlass
    {
       public static void main(String[] args)
       {
          topPart();
          middlePart();
          verticalPart();
          bottomPart();
          topPart();
       }
     
     
       //This method produces the top part of the hourglass
       public static void topPart()
       {
          System.out.print("|");
          for (int i =1; i <=10; i++)
          {
             System.out.print("\"");
          }
          System.out.print("|");
     
          System.out.println();
       }
     
       //This method produces the middle part of the hourglass
       public static void middlePart()
       {
           for (int line =1; line <=4; line++)
          {
             for (int space=1; space <= (line * 1 + 1 ); space++)
             {
                System.out.print(" ");
             }
     
             for (int semicolon =1; semicolon <= (line * -2 + 10); semicolon++)
             {
     
                System.out.print(":");
             }
     
              for (int space=1; space <= (line * 1 + 1); space++)
             {
                System.out.print(" ");
     
             }
     
             System.out.println();
           }
       }
     
       //This method produces the vertical line of the hourglass         
       public static void verticalPart()
       {
          for (int line =1; line <=1; line = (line * 5))
          {
             System.out.print("");
          }
     
          System.out.print("||");
     
          for (int line =1; line <=1; line = (line * 5))
          {
             System.out.print(" ");
          }
          System.out.println();   
       }
     
       //This method produces the bottom part of the hourglass
       public static void bottomPart()
       {   
         for(int line =1; line <=4; line++)
           {
             for (int space=1; space <= (line * -1 + 6); space++)
             {
                System.out.print(" ");
             }
     
             for (int semicolon =1; semicolon <= (line * 2); semicolon++)
             {
                System.out.print(":");
             }
     
              for (int space=1; space <= (line * -1 + 6); space++)
             {
                System.out.print(" ");
             }
     
             System.out.println();
       }
    }
    }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Displaying an hourglass

    I think you just need to look at the method verticalPart() a bit more closely to analyze what it's doing and compare that to what it should be doing. First, describe what it should be doing, then what the method is doing. Something like:

    Print x spaces
    Print '||'

    Then compare that with what you've programmed the code to do - something different.

Similar Threads

  1. need help printing hourglass shape
    By valium123 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 26th, 2014, 05:22 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. Displaying the other
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 08:58 AM
  4. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  5. Not Displaying what I need
    By rob3097 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2010, 10:31 PM