Results 1 to 7 of 7
  1. #1
    Two
    Two is offline Member
    Join Date
    Oct 2014
    Posts
    4
    Rep Power
    0

    Default Compiles correctly, but won't display

    So, I'm writing this program, and when I compile it it says Process Completed, but when I run it, it only goes through the first two if statements and won't do the case. I checked it and my brackets line up, and there obviously isn't any syntax error. The first two answers are 41 and right.

    import static java.lang.System.*;
    import javax.swing.JOptionPane;
    import java.util.*;
    public class area51
    {
    public static void main(String args[])
    {
    Scanner doctorwho = new Scanner(System.in);
    //************************* definition ***************************
    int openCage = 41;
    int tryCage = 0;
    String StryCage = "";
    String filler = "";
    String leftRight = "";
    String left = "left";
    String right = "right";
    String door = "door";
    String sleepingGuard = "";
    double shipTrial1 = 1003.84;
    double TryshipTrial1 = 0;
    String shipTrial2 = "";
    String incorrectly = "incorrectly";
    String shipTrial3 = "";
    String startScreen = "AREA 51: TRAPPED!!!!!!!!!!!!!!! " +
    "You were taking a quick flight around the galaxy one day " +
    "when you crashed on Earth. Sadly, the government commandeered you and your " +
    "ship, no doubt for nefarious purposes. But, the guard outside your prison " +
    "has fallen asleep, giving you a chance to examine the lock. You do so, and " +
    "find that, for some reason that has been forever lost to the sands of time, " +
    "you must solve a simple mathematics equation in order to unlock the door: " +
    "9*4+8-3.";
    String wrongAnswer = "You type whatever it is you typed into the ship's console." +
    "It whirrs, buzzes, sparks fly from the screen. You stand up and reach " +
    "for the door, and the last thing you hear is a long, low beep. " +
    "The ship self destructs, and when the smoke clears it's as if it was " +
    "never here.";
    String lullaby = "lullaby";
    String deathMetal = "death metal";
    String shootingatYou = "You answer the question correctly, sneak past the " +
    "sleeping guard, and move down the hall. This simply isn't your " +
    "day, however, because now people are shooting at you! Dodge " +
    "left, or right?";
    System.out.println(startScreen);
    tryCage = doctorwho.nextInt();
    StryCage = "" + tryCage;
    out.println(StryCage + " " + openCage+ "Q");
    if (tryCage == openCage)
    {
    out.println("You answer the question correctly, sneak past the " +
    "sleeping guard, and move down the hall. This simply isn't your " +
    "day, however, because now people are shooting at you! Dodge " +
    "left, or right? Choose wisely.");
    leftRight = doctorwho.next();

    if (leftRight.equalsIgnoreCase(right) == true)
    { //CURLY BRACES
    out.println("You dodge all the bullets and make it out of " +
    "there, which only gets you to yet another hallway, this one " +
    "much more dilapidated than the rest. You come upon a mysterious " +
    "and bloody door. Do you:" +
    "<1> Try to open the door." +
    "<3> Keep along your merry way." +
    "Type the number of the choice you want.");
    switch (door) {
    case "1":
    out.println("You try to open the door, only to discover " +
    "that it's locked. What a surprise here in this top secret " +
    "government area. In any case, you quickly discover that " +
    "to get the key, you'll need to steal it off of that " +
    "sleeping security guard from a bit ago. To ensure that he " +
    "stays safely in dreamland you can either play a lullaby " +
    "or metal off of your handy dandy iPod. Choose wisely." +
    "Type the name of the genre.");
    sleepingGuard = doctorwho.nextLine();
    if (sleepingGuard.equalsIgnoreCase(lullaby))
    {
    out.println("You played the lullaby. Sadly, this woke him " +
    "up, and he immediately strangled you, Simpsons style.");
    }
    else
    if (sleepingGuard.equalsIgnoreCase(deathMetal))
    { //CURLY BRACES
    out.println("You, for some reason, chose to play death " +
    "metal to keep the guard asleep, which somehow worked! " +
    "The guard slumbers peacefully as you steal the key from " +
    "him. You run back to the mysterious door and open it " +
    "only to find another member of your species, bloodied " +
    "and unconcious. Will you:" +
    "<1> Take him with you. Bros before imminent death." +
    "or, <2> Leave him. He won't know.");
    switch ("takeLeave")
    {
    case "1":
    out.println("You took him with you. While noble, that was ill-advised, " +
    "because dragging along a comatose comrade slowed you down enough for " +
    "the humans to find you and put both of you back in captivity.");
    break;
    case "2":
    out.println("You didn't take along your fallen future friend. While some " +
    "may call that heartless, we call it evolutionarily wise, because you " +
    "survived and found your ship! Do you: " +
    "<1> Repair it, OR " +
    "<2> Continue on foot? ");
    }
    switch ("repairWalk")
    {
    case "1":
    out.println("You chose to repair your ship. Smart move. Now you must solve a " +
    "short series of questions in order to activate the repair functions." +
    "Question One: " +
    "7*6/3.27+1000-9. Answer up to two decimal places. Don't add commas.");
    TryshipTrial1 = doctorwho.nextDouble();
    String Stryshiptrial1 = Double.toString(TryshipTrial1);
    if (Stryshiptrial1.equals(shipTrial1) == true)
    {
    out.println("Correct! On to the next puzzle! If D = D and E = E, " +
    "which comes first? Use uppercase.");
    shipTrial2 = doctorwho.next();
    if (shipTrial2.compareTo("D") == 0)
    {
    out.println("Correct! Last Puzzle! What English " +
    "word is always spelled incorrectly?");
    shipTrial3 = doctorwho.nextLine();
    if (shipTrial3.equalsIgnoreCase(incorrectly) == true)
    {
    out.println("Correct! Your ship takes off, and for a while it's smooth sailing. " +
    "You're considering putting it on autopilot and taking a nap until it's time " +
    "to exit the atmosphere when those ne'er-do-wells at Area 51 start shooting " +
    "at you. After a harrowing battle, your ship crashes in the Nevada desert. " +
    "You scramble out of the ship and see officers heading toward the ship. You turn " +
    "and run. Up ahead you see a cave. Will you: "+
    "<1> Go into the cave " +
    "<2> Bury yourself for camouflage until they stop looking for you. ");
    switch ("caveCamo")
    {
    case "1":
    out.println("You enter the cave. It's very dark and dank, just generally " +
    "cave-like. You walk further into the blackness, using your natural night " +
    "vision to see and just generally minding your own business, when someone" +
    "hits you in the face with a rock. \"Expletives!\", you shout. \"Language!\"," +
    "you hear. As you stand there rubbing your wound and wondering how this day" +
    "could possibly get any stranger, a spry old hermit emerges from the gloom." +
    "\"What are you doing in my cave?\" he demands. " +
    "\"What are you doing in a cave?\" you shout back. \"I asked you first!\" he says." +
    "The two of you glare at each other. Finally, the old man harrumphs and throws " +
    "a wadded up ball of paper at you. When you unfold it, you find it's a list of " +
    "numbers. When you look back up, you see the spry old hermit rapidly disappearing " +
    "into the cave. Do you follow him (type follow) or not (type no)?" );
    // if (follow
    //You're doing well for yourself.
    //else
    //You starve to death in a cave.
    }
    }
    else
    if (shipTrial3.equalsIgnoreCase(incorrectly) == false)
    {
    out.println("Incorrect! And such a shame too, you were so close to " +
    "the end. The answer was incorrectly. The word flashes on the " +
    "console screen as alarms blare and robotic arms strap you to your " +
    "chair. The words: <THIS WILL NOT TURN OUT WELL FOR YOU> scroll " +
    "across the screen. \"You're telling me,\" you say, an instant " +
    "before the ship blows up.");
    }
    }
    else
    if (shipTrial2.compareTo("E") == 0)
    {
    out.println("Incorrect. As you type E, the ship emits a long, low chuckle. " +
    "Alarms blare. Sparks fly from the console. Bars come from your seat and lock " +
    "you into place. \"This can't possibly end well.\", you think, as the ship self- " +
    "destructs.");
    }
    }
    //String STryshipTrial1 = Double.toString(TryshipTrial);
    //else if (STryshipTrial1.equals(shipTrial1) == false)
    //{
    // out.println("wrongAnswer");
    //}
    break;
    case "2":
    out.println("You decided to keep walking, past the only ship capable of taking " +
    "you home. Unsurprisingly, this did not turn out well");
    }
    }
    case "3":
    out.println("You keep moving and are eventually recaptured " +
    "and taken back to your cage thing. Reload the game to try" +
    "again.");
    break;
    //CURLY BRACE
    //CURLY BRACE
    //CURLY BRACE


    }
    } //CURLY BRACE
    else if (leftRight.equalsIgnoreCase(left) == true)
    {
    out.println("You dodged left, directly into a bullet, which " +
    "lodged iself into your head and killed you instantly. You should" +
    "have dodged right, which was right, see? It's our own paltry " +
    "attempt at humor.");
    }

    }
    else
    if (StryCage.equals(openCage) == false)
    {
    out.println("The cage didn't open, mostly because you answered " +
    "incorrrectly. Your efforts woke the guard and he stabbed you in " +
    "the face with a pitchfork, killing you instantly.");
    }
    }


    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Compiles correctly, but won't display

    First you need to place your code between code tags [code][/code]. Then make certain your code is
    properly indented.

    Your switch statements are incorrect. Switching on the variable door is okay since door is a variable. But
    you are using actual values in other switch statements which makes no sense.

    And you don't need to compare a boolean result to a boolean constant in a conditional statement. Conditional
    statements evaluate to booleans anyway so in stead of doing this:

    Java Code:
    if (a.equals("A") == true) {
       // do something
    }
    
    // you  can do this
    
    if (a.equals("A")) {
        // do something
    }
    To check for false conditions, place a ! in front of them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Two
    Two is offline Member
    Join Date
    Oct 2014
    Posts
    4
    Rep Power
    0

    Default Re: Compiles correctly, but won't display

    Thanks for telling me about the conditionals. I changed the names of the switches and it still didn't work. Do you think it's the quotation marks around the case names?

    Thanks for your help,
    Two

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Compiles correctly, but won't display

    No because then it wouldn't compile correctly. But doing something like the following is
    not what you want to do:

    Java Code:
    switch ("A") {
    // this will always hit case "A": and nothing else.
    }
    It would probably help if you reformatted your code and reposted it. Otherwise, I can only guess.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Two
    Two is offline Member
    Join Date
    Oct 2014
    Posts
    4
    Rep Power
    0

    Default Re: Compiles correctly, but won't display

    I changed the case I'm having trouble with to an if statement, and it still doesn't work. I didn't change the conditionals because it would have taken a while and the program doesn't have a problem with them.

    Here's the code:
    Java Code:
    import static java.lang.System.*;
    import javax.swing.JOptionPane;
    import java.util.*;
    public class area51
    {
    	public static void main(String args[])
    	{
    		Scanner doctorwho = new Scanner(System.in);
       //************************* definition ***************************
       int openCage = 41;
       int tryCage = 0;
       String StryCage = "";
       String filler = "";
       String leftRight = "";
       String left = "left";
       String right = "right";
       String door = "door";
       String sleepingGuard = "";
       double shipTrial1 = 	1003.84;
       double TryshipTrial1 = 	0;
       String shipTrial2 = "";
       String incorrectly = "incorrectly";	
       String shipTrial3 = "";
       String startScreen =	"AREA 51: TRAPPED!!!!!!!!!!!!!!! " +
    "You were taking a quick flight around the galaxy one day " +
    "when you crashed on Earth. Sadly, the government commandeered you and your " + 
    "ship, no doubt for nefarious purposes. But, the guard outside your prison " +
    "has fallen asleep, giving you a chance to examine the lock. You do so, and " +
    "find that, for some reason that has been forever lost to the sands of time, " +
    "you must solve a simple mathematics equation in order to unlock the door: " +
    "9*4+8-3.";
       String wrongAnswer = "You type whatever it is you typed into the ship's console." +
    									"It whirrs, buzzes, sparks fly from the screen. You stand up and reach " +
    									"for the door, and the last thing you hear is a long, low beep. " +
    									"The ship self destructs, and when the smoke clears it's as if it was " +
    									"never here.";
       String lullaby = "lullaby";
       String metal = "metal";
       String shootingatYou = "You answer the question correctly, sneak past the " +
    			"sleeping guard, and move down the hall. This simply isn't your " +
    			"day, however, because now people are shooting at you! Dodge " +
    			"left, or right?";
       String takeLeave = "takeLeave";
       String repairWalk = "repairWalk";
       String caveCamo = "caveCamo";
       String tryMove = "";	
       String trytry = "try";
       String move = "move";									
    System.out.println(startScreen);
    
    tryCage = doctorwho.nextInt();
    StryCage = "" + tryCage;
    out.println(StryCage +  " " + openCage+ "Q");
    	if (tryCage == openCage)
    		{
    			out.println("You answer the question correctly, sneak past the " +
    			"sleeping guard, and move down the hall. This simply isn't your " +
    			"day, however, because now people are shooting at you! Dodge " +
    			"left, or right? Choose wisely.");
    			leftRight = doctorwho.next();
    			
    			if (leftRight.equalsIgnoreCase(right))
    			{ //CURLY BRACES
    				out.println("You dodge all the bullets and make it out of " +
    				"there, which only gets you to yet another hallway, this one " +
    				"much more dilapidated than the rest. You come upon a mysterious " +
    				"and bloody door. Do you try to open the door (type try) or keep " +
    				"moving? (type move)");
    				tryMove = doctorwho.nextLine();
    				if (tryMove.equalsIgnoreCase(trytry))
    				{
    					out.println("You try to open the door and discover that it's locked. " +
    					"What a surprise here in this top seret government area. In any case, " +
    					"You soon discover that the key is with a sleeping guard near the door. " +
    					"You must make sure he stays securely in dreamland, and, to do so, you " +
    					"have to play some music off of your handy dandy iPod, Do you play metal or " +
    					"a lullaby? Type the name of the genre.");
    					sleepingGuard = doctorwho.nextLine();
    					if (sleepingGuard.equalsIgnoreCase(lullaby))
    					{
    						out.println("You played the lullaby. Sadly, this woke him " +
    						"up, and he immediately strangled you, Simpsons style.");
    					}
    					else
    					if (sleepingGuard.equalsIgnoreCase(metal))
    					{ //CURLY BRACES
    						out.println("You, for some reason, chose to play death " +
    						"metal to keep the guard asleep, which somehow worked! " +
    						"The guard slumbers peacefully as you steal the key from " +
    						"him. You run back to the mysterious door and open it " +
    						"only to find another member of your species, bloodied " +
    						"and unconcious. Will you:" +
    						"<1> Take him with you. Bros before imminent death." +
    						"or, <2> Leave him. He won't know.");
    						switch (takeLeave)
    						{
    						case "1":
    							out.println("You took him with you. While noble, that was ill-advised, " +
    							"because dragging along a comatose comrade slowed you down enough for " +
    							"the humans to find you and put both of you back in captivity.");
    							break;
    						case "2":
    							out.println("You didn't take along your fallen future friend. While some " +
    							"may call that heartless, we call it evolutionarily wise, because you " +
    							"survived and found your ship! Do you: " +
    							"<1> Repair it, OR " + 
    							"<2> Continue on foot? ");
    						}
    							switch (repairWalk)
    							{
    							case "1":
    								out.println("You chose to repair your ship. Smart move. Now you must solve a " +
    								"short series of questions in order to activate the repair functions." +
    								"Question One: " +
    								"7*6/3.27+1000-9. Answer up to two decimal places. Don't add commas.");
    								TryshipTrial1 = doctorwho.nextDouble();
    								String Stryshiptrial1 = Double.toString(TryshipTrial1);
    								if (Stryshiptrial1.equals(shipTrial1) == true)
    								{
    									out.println("Correct! On to the next puzzle! If D = D and E = E, " +
    									"which comes first? Use uppercase.");
    									shipTrial2 = doctorwho.next();
    									if (shipTrial2.compareTo("D") == 0)
    									{
    										out.println("Correct! Last Puzzle! What English " +
    										"word is always spelled incorrectly?");
    										shipTrial3 = doctorwho.nextLine();
    										if (shipTrial3.equalsIgnoreCase(incorrectly) == true)
    										{
    											out.println("Correct! Your ship takes off, and for a while it's smooth sailing. " +
    											"You're considering putting it on autopilot and taking a nap until it's time " + 
    											"to exit the atmosphere when those ne'er-do-wells at Area 51 start shooting " +
    											"at you. After a harrowing battle, your ship crashes in the Nevada desert. " +
    											"You scramble out of the ship and see officers heading toward the ship. You turn " +
    											"and run. Up ahead you see a cave. Will you: "+
    											"<1> Go into the cave " +
    											"<2> Bury yourself for camouflage until they stop looking for you. ");
    											switch (caveCamo)
    											{
    											case "1":
    												out.println("You enter the cave. It's very dark and dank, just generally " +
    												"cave-like. You walk further into the blackness, using your natural night " +
    												"vision to see and just generally minding your own business, when someone" +
    												"hits you in the face with a rock. \"Expletives!\", you shout. \"Language!\"," +
    												"you hear. As you stand there rubbing your wound and wondering how this day" +
    												"could possibly get any stranger, a spry old hermit emerges from the gloom." +
    												"\"What are you doing in my cave?\" he demands. " +
    												"\"What are you doing in a cave?\" you shout back. \"I asked you first!\" he says." +
    												"The two of you glare at each other. Finally, the old man harrumphs and throws " +
    												"a wadded up ball of paper at you. When you unfold it, you find it's a list of " +
    												"numbers. When you look back up, you see the spry old hermit rapidly disappearing " +
    												"into the cave. Do you follow him (type follow) or not (type no)?" );
    												// if (follow
    												//You're doing well for yourself.
    												//else
    												//You starve to death in a cave.
    											}
    										}
    										else
    										if (shipTrial3.equalsIgnoreCase(incorrectly) == false)
    										{
    											out.println("Incorrect! And such a shame too, you were so close to " +
    											"the end. The answer was incorrectly. The word flashes on the " +
    											"console screen as alarms blare and robotic arms strap you to your " +
    											"chair. The words: <THIS WILL NOT TURN OUT WELL FOR YOU> scroll " + 
    											"across the screen. \"You're telling me,\" you say, an instant " +
    											"before the ship blows up.");  
    										}
    									}
    									else
    									if (shipTrial2.compareTo("E") == 0)
    									{
    										out.println("Incorrect. As you type E, the ship emits a long, low chuckle. " +
    										"Alarms blare. Sparks fly from the console. Bars come from your seat and lock " +
    										"you into place. \"This can't possibly end well.\", you think, as the ship self- " +
    										"destructs.");
    									}
    								}
    								//String STryshipTrial1 = Double.toString(TryshipTrial);	
    								//else if (STryshipTrial1.equals(shipTrial1) == false)
    								//{
    								//	out.println("wrongAnswer");
    								//}
    							break;
    							case "2":
    							out.println("You decided to keep walking, past the only ship capable of taking " +
    							"you home. Unsurprisingly, this did not turn out well");
    							}
    						}
    				else
    				if (tryMove.equalsIgnoreCase(move))
    				{
    					out.println("You chose to move on, and quickly get lost. The management finds " +
    					"you a week later after you accidentally find yourself back in your original " +
    					"cage.");
    				}
    				
    			
    		    }
    		    } //CURLY BRACE
    			else if (leftRight.equalsIgnoreCase(left) == true)
    			{
    				out.println("You dodged left, directly into a bullet, which " +
    				"lodged iself into your head and killed you instantly. You should" +
    				"have dodged right, which was right, see? It's our own paltry " +
    				"attempt at humor.");
    			}
    		
    	
    	//CURLY BRACE
    	}
    	else
    	if  (StryCage.equals(openCage) == false)
    	{
    		out.println("The cage didn't open, mostly because you answered " + 
    		"incorrrectly. Your efforts woke the guard and he stabbed you in " +
    		"the face with a pitchfork, killing you instantly.");
    	}
    }	
    	//CURLY BRACE		
    
    }  //CURLY BRACE
    (I tried to enter it as code, it may or may not have worked.)
    Thanks for all the help,
    Two

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Compiles correctly, but won't display

    You need to tell me which if statement is the problem and what the problem is. Also, how is user input entered?
    When using next(), nextInt() or nextLine() you need to understand what is returned. For example, if you use a
    next() followed by nextLine() you won't get the nextLine. You will get what is left of the current line upto and excluding
    the line terminator. This is because the line terminator is still in the input buffer for the current line.
    That may not be what you want. You may want to play around with the Scanner class on a smaller scale to understand
    some of its unexpected behavior.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Two
    Two is offline Member
    Join Date
    Oct 2014
    Posts
    4
    Rep Power
    0

    Default Re: Compiles correctly, but won't display

    Never mind, I fixed it by switching all my .nextLine() blocks to .next() blocks.

    Thanks for all your help,
    Two

Similar Threads

  1. Replies: 2
    Last Post: 10-08-2013, 12:06 AM
  2. Some letters don't display correctly
    By mcdhappy80 in forum JDBC
    Replies: 10
    Last Post: 04-25-2011, 10:00 PM
  3. image correctly loaded, but JPanel wont display
    By apchar in forum AWT / Swing
    Replies: 3
    Last Post: 04-13-2011, 03:15 AM
  4. compiles; no display
    By skuzzie in forum New To Java
    Replies: 5
    Last Post: 12-18-2010, 07:57 PM
  5. Program Compiles but Buttons do not display
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-11-2008, 03:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •