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: Java HSA Break To Part of Code

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java HSA Break To Part of Code

    So in class, I have to do a text based game in HSA console. It's been alright except for one error. When the user inputs something the program doesn't understand, I want it to print "That command is not recognized" and reprint the part of story they were at before instead of going back to the beginning to start over again. I've done it by assigning a value to a variable under every different part of the story. But now when I try to break back to the part of the code, it says that some of the things are an undeclared variable.

    This is my code for this particular section:
    hallway:
                if (key_entered == 'S' || key_entered == 's')
                {
                    c.print ("\n\nYou are standing in a dark hallway. There is a door to the north, and a light coming from the west. ");
                    error = 1;
     
                    directionHallway = c.readString ();
     
     
     
                    door:
                    if (directionHallway.equals ("north"))
                    {
                        c.print ("You approach the door and stare at it. A while passes.");
                        c.print ("\nAnd then you realize it's locked. There's a keyhole on the door. ");
                        error = 2;
                        directionDoor = c.readLine ();
     
     
     
                        if (directionDoor.equals ("open door") && (key = false))
                        {
                            c.print ("It's locked.");
                        }
     
                        else if (directionDoor.equals ("open door") && (key = true))
                        {
                            c.print ("The door slowly creaks open and you enter.");
                        }
     
     
     
                    }
     
                    blankwall:
                    if (directionHallway.equals ("east"))
                    {
                        c.print ("There is simply a blank wall to the east. Nothing to do but backtrack to where you were. ");
                        error = 3;
                        directionBlankWall = c.readLine ();
     
     
                    }
     
                    haggis:
                    if (directionHallway.equals ("south"))
                    {
                        c.print ("There is a wall that is devoid of anything but a poster. It simply says 'Haggis'. ");
                        error = 4;
                        directionHaggis = c.readLine ();
                    }
     
                    statueroom:
                    if (directionHallway.equals ("west"))
                    {
                        c.print ("The light leads to a room with a strange statue.");
                        c.print ("\nThere are doors to the north, west, and south. ");
                        error = 5;
                        directionStatueRoom = c.readLine ();
                    }
     
                    else
                    {
                        c.print ("That command is not recognized.");
     
                        if (error == 1)
                        {
                            break hallway;
                        }
     
                        if (error == 2)
                        {
                            break door;
                        }
     
                        if (error == 3)
                        {
                            break blankwall;
                        }
     
                        if (error == 4)
                        {
                            break haggis;
                        }
     
                        if (error == 5)
                        {
                            break statueroom;
                        }
     
                    }
     
     
                }

    The only ones that are being highlighted as undeclared labels are the door, blankwall, and haggis. Anyone know what's wrong? And yeah, I'm a total beginner at this.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: Java HSA Break To Part of Code

    Does that code compile?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java HSA Break To Part of Code

    It compiles but not successfully. When I put it with the rest of the code, it says that it can't break to door, blankwall, and haggis because they are undeclared labels.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: Java HSA Break To Part of Code

    Yeah, I don't think you can do that sort of thing in Java (I could be wrong though). I know you can label loops, and use break label; to leave an outer loop, but I don't think you can do the sort of thing you are trying. To me, it seems you are attempting to use the break keyword as a goto (in C), which it is not. Gotos are not supported in Java regardless.

    The best thing I can think of is getting rid of the labels and turning each block of code that you tried to label into individual methods, and then call those methods.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java HSA Break To Part of Code

    Ok makes sense. Can you just give me a quick example of how to do that and calling the individual methods? We didn't learn this and it'd be helpful. Thanks.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java HSA Break To Part of Code

    When you want to do something a certain number of times, but are not sure how many, consider the while loop

    In this case you want to ...print "That command is not recognized" and reprint the part of story... while ...the user gives bad commands...

    Looking at the bigger picture you also want to ...tell a part of the story... while ...there are more parts of the story to tell...

Similar Threads

  1. What does this part of the code mean?
    By jean28 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 24th, 2012, 12:47 AM
  2. What does this short part of a code do?
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2012, 12:31 PM
  3. Replies: 1
    Last Post: June 28th, 2011, 08:34 AM
  4. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 04:10 PM
  5. How to write to specific part of an html file using java
    By nasi in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: May 28th, 2010, 12:22 AM

Tags for this Thread