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

Thread: Displaying an hourglass 2

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

    Default Displaying an hourglass 2

    Hello,

    I am working on the same code to produce the Hour Glass figure.

    My code is a little bit different because I am using a CONSTANT to change the size of the Hour Glass. I have my CONSTANT set to 4. The problem I am having is in my method for the BOTTOM HALF.
    I am getting an error message that says this.
    HourGlass.java:56: error: class, interface, or enum expected
    	public static void drawBottomHalf()
    What I don't understand is that to me it look like the same as the Top Half but with reverse order so I don't understand why I am getting this error.

    Here is my code.
    public class HourGlass
    {
    	public static final int HEIGHT = 4;
     
    	public static void main(String[] args)
    	{
    		drawTop();
    		drawTopHalf();
    		drawVertical();
    		drawBottomHalf();
    		drawTop();
    	}
     
    	// Produces the top part of the hour glass
    	public static void drawTop()
    	{
    		System.out.print("|");
    		for (int i = 1; 1 <= (2 * HEIGHT + 1); i++)
    		{
    			System.out.print("\"");
    		}
    		System.out.println("|");
    	}
     
    	// Produces the top half of the hour glass
    	public static void drawTopHalf()
    	{
    		for (int line = 1; line <= HEIGHT; line++)
    		{
    			for (int i = 1; i <= (line-1); i++)
    			{
    			System.out.print(" ");
    			}
    			System.out.print("\\");
    			int dots = 2 * HEIGHT - 2 * line;
    			for (int i = 1; i <= dots; i++)
    			{
    				System.out.print(":");
    			}
    			System.out.print("/");
    			}
    	}
    	// Produces the Vertical two line in the center
    	public static void drawVertical()
    	{
    		System.out.print("|");
    		System.out.print(" ");
    		system.out.print("|");
    		}
    	}
    	// Produces the bottom half of the hour glass
    	public static void drawBottomHalf()
    	{
    		for (int line = 1; line <= HEIGHT; line++)
    		{
    			for (int i = 1; i <= (HEIGHT - line); i++)
    			{
    				System.out.print(" ");
    			}
    			System.out.print("/")
    			for (int i = 1; i <= 2 * (line-1); i++)
    			{
    				System.out.print(":");
    			}
    			System.out.print("\\");
    			for (int i = 1; i <= (HEIGHT - line);
    			{
    				System.out.print(" ");
    			}
    		}
    	}
    }


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hour Glass

    Hello Everyone,

    I am trying to print out an Hour Glass figure using CONSTANTS so that I can change sizes from 4 to 10. The problem I am having is that I am getting a error message for my method called for on the bottom half.
    Here is the error message.
    HourGlass.java:56: error: class, interface, or enum expected
    	public static void drawBottomHalf()
    	              ^
    HourGlass.java:58: error: class, interface, or enum expected
    		for (int line = 1; line <= HEIGHT; line++)

    Here is my code for the hour glass figure.
    public class HourGlass
    {
    	public static final int HEIGHT = 4;
     
    	public static void main(String[] args)
    	{
    		drawTop();
    		drawTopHalf();
    		drawVertical();
    		drawBottomHalf();
    		drawTop();
    	}
     
    	// Produces the top part of the hour glass
    	public static void drawTop()
    	{
    		System.out.print("|");
    		for (int i = 1; 1 <= (2 * HEIGHT + 1); i++)
    		{
    			System.out.print("\"");
    		}
    		System.out.println("|");
    	}
     
    	// Produces the top half of the hour glass
    	public static void drawTopHalf()
    	{
    		for (int line = 1; line <= HEIGHT; line++)
    		{
    			for (int i = 1; i <= (line-1); i++)
    			{
    			System.out.print(" ");
    			}
    			System.out.print("\\");
    			int dots = 2 * HEIGHT - 2 * line;
    			for (int i = 1; i <= dots; i++)
    			{
    				System.out.print(":");
    			}
    			System.out.print("/");
    			}
    	}
    	// Produces the Vertical two line in the center
    	public static void drawVertical()
    	{
    		System.out.print("|");
    		System.out.print(" ");
    		system.out.print("|");
    		}
    	}
    	// Produces the bottom half of the hour glass
    	public static void drawBottomHalf()
    	{
    		for (int line = 1; line <= HEIGHT; line++)
    		{
    			for (int i = 1; i <= (HEIGHT - line); i++)
    			{
    				System.out.print(" ");
    			}
    			System.out.print("/")
    			for (int i = 1; i <= 2 * (line-1); i++)
    			{
    				System.out.print(":");
    			}
    			System.out.print("\\");
    			for (int i = 1; i <= (HEIGHT - line);
    			{
    				System.out.print(" ");
    			}
    		}
    	}

    I can't seem to see how this bottom half of the method is different from the top have of the method. So I don't understand what the error is or why it is giving me this error.

    Any suggestions on how to fix this would be great.

    Thanks.

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

    Default Re: Displaying an hourglass 2

    Post moved to own thread.
    HourGlass.java:56: error: class, interface, or enum expected
    Check that the {}s are properly paired somewhere before the line where the error message points.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    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: Hour Glass

    Methods should be inside classes but NOT inside other methods. Check that the '{' and '}' match up correctly.

    I think Norm helped, but please don't make multiple posts for the same topic. Threads merged into one.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Displaying an hourglass 2

    Ok that worked. I had one too many } brackets above it.

    Thanks

    --- Update ---

    Well,

    Once I fix the bracket issue for my Bottom Half method. I tried to finish the code by adding the top part of the method again to the bottom. Now I am getting an error that says
    HourGlass.java:79: error: illegal start of expression
    	public static void drawTop()
    	^
    HourGlass.java:79: error: illegal start of expression
    	public static void drawTop()

    I have move and added brackets above the expression and to me it seems that all my methods are lined up under class and that each method is closed off. Unless I am not see it. Here is my code with the added Top half to the bottom of it.
    public class HourGlass
    {
    	public static final int HEIGHT = 4;
     
    	public static void main(String[] args)
    	{
    		drawTop();
    		drawTopHalf();
    		drawVertical();
    		drawBottomHalf();
    		drawTop();
    	}
     
    	// Produces the top part of the hour glass
    	public static void drawTop()
    	{
    		System.out.print("|");
    		for (int i = 1; 1 <= (2 * HEIGHT + 1); i++)
    		{
    			System.out.print("\"");
    		}
    		System.out.println("|");
    	}
     
    	// Produces the top half of the hour glass
    	public static void drawTopHalf()
    	{
    		for (int line = 1; line <= HEIGHT; line++)
    		{
    			for (int i = 1; i <= (line-1); i++)
    			{
    			System.out.print(" ");
    			}
    			System.out.print("\\");
    			int dots = 2 * HEIGHT - 2 * line;
    			for (int i = 1; i <= dots; i++)
    			{
    				System.out.print(":");
    			}
    			System.out.print("/");
    			}
    	}
     
    	// Produces the Vertical two line in the center
    	public static void drawVertical()
    	{
    		System.out.print("|");
    		System.out.print(" ");
    		system.out.print("|");
     
    	}
     
    	// Produces the bottom half of the hour glass
    	public static void drawBottomHalf()
    	{
    		for (int line = 1; line <= HEIGHT; line++)
    		{
    			for (int i = 1; i <= (HEIGHT - line); i++)
    			{
    				System.out.print(" ");
    			}
    			System.out.print("/");
    			for (int i = 1; i <= 2 * (line-1); i++)
    			{
    				System.out.print(":");
    			}
    			System.out.print("\\");
    			for (int i = 1; i <= (HEIGHT - line); i++)
    			{
    			System.out.print(" ");
     
    	}
     
    	// Produces the top part of the hour glass
    	public static void drawTop()
    	{
    		System.out.print("|");
    		{
    		for (int i = 1; 1 <= (2 * HEIGHT + 1); i++)
    		{
    			System.out.print("\"");
    		}
    		System.out.println("|");
    	}
    }

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

    Default Re: Displaying an hourglass 2

    HourGlass.java:79: error: illegal start of expression
    Looks like you need to check the {}s again.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Displaying an hourglass 2

    yeah I have tried moving adding and deleting {} and I can not get it to work. I don's understand this simple process of {}. I know it is used to group together related bits of code. I open and closed off my other method so why I am getting this error.

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

    Default Re: Displaying an hourglass 2

    why I am getting this error.
    Because the {}s are not properly paired.
    If you can't find the mismatched pair, a couple of suggestions:
    1)print the source file and use a pencil to connect every { with it's }
    2)add a comment with a number to each { and add a comment with the same number of the pairing }
    3)Some IDEs have a Find the pairing symbol function: place the cursor on one {, press some keys (mine is CTRL+]) and the cursor moves to the pairing }
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    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 2

    There are one too many '{'s in the last drawTop() method, AND there are too many drawTop() methods. A single method called drawTop() can be called multiple times, but multiple methods called drawTop() is not allowed.

    There are TWO too few '}'s in drawBottom().

    There is also one 'system' that should be "System".

    What editor are you using?

  10. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Displaying an hourglass 2

    jGRASP

  11. #11
    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 2

    I don't know it, but I found this tip that might be helpful:

    Cleaning-Up Indenting: jGrasp does not have a command that re-indents code, but you can trick it into doing so.
    Click on View+Generate CSD (or the button).
    Click on View+Remove CSD (or the button).

    Since the graphics are lost, the tip is here.

Similar Threads

  1. Displaying an hourglass
    By Just A Fish42 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 28th, 2014, 02:49 PM
  2. 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
  3. Hourglass using recursion
    By nWeid1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 1st, 2012, 10:30 PM
  4. Displaying the other
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 08:58 AM
  5. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM