0

I read that you can use labels to control which loops to break and continue if you have nested loops. I tried doing that in the code below, but when the outer loop is supposed to break when the cabinet is selected, it doesn't and just loops back the dining room loop, only breaking the kitchen loop.

//Same imports used from the original code as needed. import java.net.MalformedURLException; import java.util.Scanner; import javax.swing.JOptionPane; import java.net.URL; import javax.swing.ImageIcon; public class LoopyHauntedHouse { private String name; //Player name. private String location0; //First location selected from door. private String toFrontDoor = ""; private String atFrontDoor; //Method asking for user's name, and welcoming the user as well as asking where they'd like to go for the first time. public void intro()throws MalformedURLException { //URL to image initialized from the original code as needed. URL frontDoor = new URL("http://i.imgur.com/2m3giQk.png"); //Scanner for user input. Scanner scnr = new Scanner(System.in); //Asking for user's name and welcoming the user. name = JOptionPane.showInputDialog(null, "What is your name?"); JOptionPane.showMessageDialog(null, "Welcome " + name + " to the Haunted House!"); //Shows starting location. JOptionPane.showMessageDialog(null, name + " you're at the front door of the haunted house.", "Title", JOptionPane.PLAIN_MESSAGE, new ImageIcon(frontDoor)); //Asks for first choice of room to start at. location0 = JOptionPane.showInputDialog(null, name + " where to next? 'Living Room', 'Dining Room' or 'Stairs'?"); } //Method for the rest of the program allowing users to walk through the house, backtrack, and interact with objects. public void startWalking()throws MalformedURLException { //URLs to images are initialized from the original code as needed. URL stairs = new URL("http://i.imgur.com/WuddJUc.png"); URL bedroom1 = new URL("http://i.imgur.com/HZ6OSyZ.png"); URL bedroom2 = new URL("http://i.imgur.com/JZORNpd.png"); URL bathroom = new URL("http://i.imgur.com/onSEc1J.png"); URL masterBedroom = new URL("http://i.imgur.com/bf4L0sH.png"); URL masterBathroom = new URL("http://i.imgur.com/yp87dTX.png"); URL livingRoom = new URL("http://i.imgur.com/7XQD5Pt.png"); URL bathRoom = new URL("http://i.imgur.com/G0CxjSy.png"); URL diningRoom = new URL("http://i.imgur.com/gyU9mep.png"); URL kitchen = new URL("http://i.imgur.com/tTMRCID.png"); URL pantry = new URL("http://i.imgur.com/zBxPJCs.png"); while(location0.equalsIgnoreCase("Living Room")||(toFrontDoor.equalsIgnoreCase("Living Room"))) { JOptionPane.showMessageDialog(null, name + " you are now in the Living Room"); String move1 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Chest' walk to the 'Bathroom' or 'Go back' and go to another room?"); if(move1.equalsIgnoreCase("Chest")) { JOptionPane.showMessageDialog(null, name + " a ghost escapes and scares you to death!"); JOptionPane.showMessageDialog(null, "Game Over! You've died.", "Try Again.", JOptionPane.WARNING_MESSAGE, new ImageIcon(livingRoom)); break; } else if(move1.equalsIgnoreCase("Bathroom")) { while(move1.equalsIgnoreCase("Bathroom")) { JOptionPane.showMessageDialog(null, name + " you are now in the bathroom."); String move2 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Mirror', 'Shower', or 'Go back'?"); if(move2.equalsIgnoreCase("Shower")) { JOptionPane.showMessageDialog(null, name + " the room suddenly steams up and you feel fingers touching the back of your neck..."); } else if(move2.equalsIgnoreCase("Mirror")) { JOptionPane.showMessageDialog(null, name + "you see a bloody face looking back at you!"); } else if(move2.equalsIgnoreCase("Go back")) { break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else if(move1.equalsIgnoreCase("Go back")) { toFrontDoor = move1; break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } outerloop: while(location0.equalsIgnoreCase("Dining Room")) { JOptionPane.showMessageDialog(null, name + " you are now in the Dining Room"); String move1 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Candelabra', walk to the 'Kitchen', or 'Go back'?"); if(move1.equalsIgnoreCase("Candelabra")) { JOptionPane.showMessageDialog(null, "The candelabra light up by themselves and " + name + " sees a death shadow!"); JOptionPane.showMessageDialog(null, "Game Over! You've died.", "Try Again.", JOptionPane.WARNING_MESSAGE, new ImageIcon(diningRoom)); break; } else if(move1.equalsIgnoreCase("Kitchen")) { while(move1.equalsIgnoreCase("Kitchen")) { JOptionPane.showMessageDialog(null, name + " you are now in the 'Kitchen'."); String move2 = JOptionPane.showInputDialog(null, name + " would you like to explore either the 'Refrigerator', the 'Cabinet', walk to the 'Pantry', or 'Go back'"); if(move2.equalsIgnoreCase("Refrigerator")) { JOptionPane.showMessageDialog(null, name + " opens the refrigerator and finds some delicious soul food."); } else if(move2.equalsIgnoreCase("Cabinet")) { JOptionPane.showMessageDialog(null, "The dished and glasses start flying at you as soon as you open the door. " + name + " gets hit in the head and feels themselves moving towards a light."); JOptionPane.showMessageDialog(null, "Game Over! You've died.", "Try Again.", JOptionPane.WARNING_MESSAGE, new ImageIcon(kitchen)); break outerloop; } else if(move2.equalsIgnoreCase("Pantry")) { while(move2.equalsIgnoreCase("Pantry")) { JOptionPane.showMessageDialog(null, name + " you are now in the Pantry."); String move3 = JOptionPane.showInputDialog(null, name + " would like to explore the 'Dusty Recipe Box', the 'Broom', or 'Go back'?"); if(move3.equalsIgnoreCase("Dusty Recipe Box")) { JOptionPane.showMessageDialog(null, name + "opens it up and a recipe for chocolate devils food cake appears out of no where."); } else if(move3.equalsIgnoreCase("Broom")) { JOptionPane.showMessageDialog(null, "As soon as " + name + " touches the broom, it flies up in the air!"); } else if(move3.equalsIgnoreCase("Go back")) { break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else if(move2.equalsIgnoreCase("Go back")) { break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else if(move1.equalsIgnoreCase("Go back")) { toFrontDoor = move1; break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } while(location0.equalsIgnoreCase("Stairs")) { JOptionPane.showMessageDialog(null, name + " you are now in font of the stairs."); String move1 = JOptionPane.showInputDialog(null, name + " would you like to walk to 'Bedroom 1', 'Bedroom 2', the 'Master Bedroom', or 'Go back'?"); if(move1.equalsIgnoreCase("Master Bedroom")) { while(move1.equalsIgnoreCase("Master Bedroom")) { JOptionPane.showMessageDialog(null, name + " you are now in the Master Bedroom."); String move2 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Jewelry Box', walk into the 'Master Bathroom', or 'Go back'?"); if(move2.equalsIgnoreCase("Jewelry Box")) { JOptionPane.showMessageDialog(null, name + " you find the cursed Hope Diamond and feel your doom."); } else if(move2.equalsIgnoreCase("Go back")) { break; } else if(move2.equalsIgnoreCase("Master Bathroom")) { while(move2.equalsIgnoreCase("Master Bathroom")) { JOptionPane.showMessageDialog(null, name + " you are now in the Master Bathroom."); String move3 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Intricate Oil Lamp', the 'Shower', or 'Go back'?"); if(move3.equalsIgnoreCase("Intricate Oil Lamp")) { JOptionPane.showMessageDialog(null, name + " you rub the lamp and a genie pops out who says he'll grant you 3 wishes!"); } else if(move3.equalsIgnoreCase("Shower")) { JOptionPane.showMessageDialog(null, name + " you suddenly hear singing in the shower but no one is there."); } else if(move3.equalsIgnoreCase("Go back")) { break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else if(move1.equalsIgnoreCase("Go back")) { toFrontDoor = move1; break; } else if(move1.equalsIgnoreCase("Bedroom 1")) { } else if(move1.equalsIgnoreCase("Bedroom 2")) { } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } public void toFrontDoor() throws MalformedURLException { if(toFrontDoor.equalsIgnoreCase("Go back")) { atFrontDoor = JOptionPane.showInputDialog(null, name + " where to next? 'Living Room', 'Dining Room', 'Stairs', or 'Leave the house'?"); if(atFrontDoor.equalsIgnoreCase("Leave the house")) { JOptionPane.showMessageDialog(null, "Game Over! Thanks for playing."); } } else { startWalking(); } } } 
7
  • @karlphillip. No, the OP is using break outerloop to break the outer loop. Commented Nov 15, 2014 at 17:31
  • Does the message "The dished and glasses start flying at you..." appear? Commented Nov 15, 2014 at 17:32
  • @aetheria it does, and then it loops back to the dining room while loop. Commented Nov 15, 2014 at 17:35
  • @aetheria Aha! Now it's easier to understand what the OP means with a more complete code snippet. I see your point. Commented Nov 15, 2014 at 17:47
  • It's still not the full source, e.g. startWalking() does not appear to be called by anything and how it is called is what we need to know about. Is it called in a loop? Commented Nov 15, 2014 at 17:51

1 Answer 1

1

Your understanding of labels is correct. The break outerloop statement should break the outer loop, as demonstrated by this example:

public class Example { public static void main(String[] args) { outerLoop: while (true) { int i = 0; while (true) { System.out.println(i++); if (i > 5) { break outerLoop; } if (i > 10) { break; } } System.out.println("Broken inner loop"); } System.out.println("Broken outer loop"); } } 

Which outputs:

0 1 2 3 4 5 Broken outer loop 

I suspect that the break outerloop statement in your code isn't actually being run. Or there's another loop that isn't visible in the code you posted that causes the outerloop to be run again?

I'd suggest simplifying the code (e.g. by extracting some methods) and/or stepping through it with a debugger. At 142 lines your startWalking() method is starting to become unwieldy. If you keep your methods below 10 lines (the book Refactoring by Martin Fowler explains how) it makes things a lot easier to see the logic (and bugs in the logic).

I find writing a simpler example to demonstrate the problem is often a good way to solve these kinds of weird issues. If you find the simple example works, you can gradually add back in the complexity until it breaks again and then you know exactly what caused it.

Sign up to request clarification or add additional context in comments.

7 Comments

I edited my post so my whole program is posted, do you think it could be affected by any of the other while loops I have there? Because for me the break outerloop doesn't seem to function, then again I do have a lot of while statements there.
To check, you can show a dialog message before and after the outerloop - a bit like I did the System.out.println("Broken outer loop"). If those messages keep repeating then you'll know you're going back into the outer loop again.
Okay I am going to try that.
Okay, so this is really weird. The outer dining room loop asks the user where they want to go, i press Kitchen, then i enter the kitchen loop, it asks what object you want to explore, I press cabinet, then it is supposed to break out of the outer loop, the dining room loop, to end the program, it doesn't, instead it goes back to the dining room loop asking again where to go, i do the same thing over, but this time it works. Reeaaallly weird, on the second time around it works but the first not. Maybe a save and restart of netbeans fixes it, it seems like the IDE is just slow maybe?
It's almost never a problem with the IDE. It'll be a bug in your code somewhere, I guarantee it!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.