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

Thread: This Works In My Other Code But Not This One?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default This Works In My Other Code But Not This One?

    I have been working on a ZORK type game in Java. I am a beginner/intermediate. Kind of right on the border. I think the problem is where the Else if(inputNumber == 9). If anyone sees anything wrong, just tell me.

    Note that this is only one part of the code










    do{
    int inputNumber = keyboard.nextInt();
    if(inputNumber == 0){
    if(start = true){
    out.println("YOU HAVE JUST LEFT DUNDEE MIDDLE SCHOOL.");
    out.println("YOU ARE RIDING HOME ON THE BUS WHEN ALL");
    out.println("OF A SUDDEN YOU SEE A MAJOR TRAFFIC JAM");
    out.println("AW MAN! YOU THINK. BUT THEN YOU SEE THE");
    out.println("CRATER IN THE GROUND. THE BUS DRIVER OPENS");
    out.println("THE DOORS AND EVERYONE RUNS OUT. YOU RUN");
    out.println("UNTIL YOU REACH YOUR HOME. ITS DESERTED.");
    out.println("YOU REALIZE YOU HAVE TO FIGURE OUT WHAT");
    out.println("IS GOING ON");
    out.println("");
    out.println("PRESS 9 TO GET YOUR OPTIONS");

    }else
    if(inputNumber == 9){
    if(home == true){
    out.println("PRESS 1 TO GO NORTH INTO HOUSE");
    out.println("PRESS 2 TO GO EAST TO INTERSECTION");
    out.println("PRESS 3 TO GO SOUTH TO PARK");
    out.println("PRESS 4 TO GO WEST TO SHOP");
    home = true;


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: This Works In My Other Code But Not This One?

    For me it's hard to tell what's wrong without more description of the mis-behavior and without seeing a bit more of the code.

    From a practical coding standpoint, if this were my program I'd strive to separate the data from the code. For instance, the game map, instructions, and items found are mainly data, not code. In other words, try to make your code as flexible as possible so that it would allow you to be able to change the map and items, their location, etc, without having to change any code.

  3. #3
    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: This Works In My Other Code But Not This One?

    Hi Ooogel

    Please use [code=java] before your code and [/code] after your code.
    You said you think there is a problem in a specific part of the code, but never said what the problem is. With the snippet provided there is no way to test the code to determine what (if any) the problem is. (As stated by curmudgeon)

    If you still have a problem please post the question and code (inside tags!) again.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: This Works In My Other Code But Not This One?

    Quote Originally Posted by Ooogel View Post
    .
    .
    .
    do{
    		int inputNumber = keyboard.nextInt();
    	if(inputNumber == 0){
    		if(start = true){
    		out.println("YOU HAVE JUST LEFT DUNDEE MIDDLE SCHOOL.");
    		out.println("YOU ARE RIDING HOME ON THE BUS WHEN ALL");
    		out.println("OF A SUDDEN YOU SEE A MAJOR TRAFFIC JAM");
    		out.println("AW MAN! YOU THINK. BUT THEN YOU SEE THE");
    		out.println("CRATER IN THE GROUND. THE BUS DRIVER OPENS");
    		out.println("THE DOORS AND EVERYONE RUNS OUT. YOU RUN");
    		out.println("UNTIL YOU REACH YOUR HOME. ITS DESERTED.");
    		out.println("YOU REALIZE YOU HAVE TO FIGURE OUT WHAT");
    		out.println("IS GOING ON");
    		out.println("");
    		out.println("PRESS 9 TO GET YOUR OPTIONS");
     
    	}else
    		if(inputNumber == 9){
    			if(home == true){
    			out.println("PRESS 1 TO GO NORTH INTO HOUSE");
    			out.println("PRESS 2 TO GO EAST TO INTERSECTION");
    			out.println("PRESS 3 TO GO SOUTH TO PARK");
    			out.println("PRESS 4 TO GO WEST TO SHOP");
    			home = true;
    By adding code tags to your original code so that I can check out your formatting, I think I may see a problem. Could be "the" problem, I'm thinking...

    Anyhow, in spite of your indentation, the 'else' goes with the "if(start)", not with the "if(inputNumber == 0)"

    Java compilers ignore indentation. And it's only convenient for Humans if the style results in something that is visually consistent with the logical organization.

    Anyhow...

    Here: Ill take your code with no changes other than a more spread-out style of indentation that is consistent with the way that the compiler sees it. (Well, I added some closing braces that will occur later on in your code, too.)
        do{
            int inputNumber = keyboard.nextInt();
            if(inputNumber == 0)
            {
                if(start = true)
                {
                    out.println("YOU HAVE JUST LEFT DUNDEE MIDDLE SCHOOL.");
                    out.println("YOU ARE RIDING HOME ON THE BUS WHEN ALL");
                    out.println("OF A SUDDEN YOU SEE A MAJOR TRAFFIC JAM");
                    out.println("AW MAN! YOU THINK. BUT THEN YOU SEE THE");
                    out.println("CRATER IN THE GROUND. THE BUS DRIVER OPENS");
                    out.println("THE DOORS AND EVERYONE RUNS OUT. YOU RUN");
                    out.println("UNTIL YOU REACH YOUR HOME. ITS DESERTED.");
                    out.println("YOU REALIZE YOU HAVE TO FIGURE OUT WHAT");
                    out.println("IS GOING ON");
                    out.println("");
                    out.println("PRESS 9 TO GET YOUR OPTIONS");
     
                } // Goes with if(start...)
                else if(inputNumber == 9)
                {
                    if(home == true){
                        out.println("PRESS 1 TO GO NORTH INTO HOUSE");
                        out.println("PRESS 2 TO GO EAST TO INTERSECTION");
                        out.println("PRESS 3 TO GO SOUTH TO PARK");
                        out.println("PRESS 4 TO GO WEST TO SHOP");
                        home = true;
                        .
                        .
                        .
                    }// Added by Zaphod_b: Goes with if (home == true)
                    .
                    .
                    .
                } // Added by Zaphod_b: Goes with else if(inputNumber == 9)
            } // Added by Zaphod_b: Goes with if(inputNumber == 0)
        } while (Loop continuation boolean expression goes here); // Added by Zaphod_b: End of the do { stuff

    Putting human-oriented comments on closing braces makes it easier for others to proofread and see if the code is consistent with your expressed intent. (Makes it easier for you to remember what you had in mind if you ever revisit the code for purposes of debugging or feature-enhancement or whatever.)

    Bottom line (just a suggestion, really): If your text editor has features that let you see matching braces, turn on that feature. If it doesn't have that feature, find one that does. If your editor has an "autoindent" feature, turn it on and don't fight it. If it doesn't have that feature find one that does. You will thank me later.

    Of course if that organization is not what you had in mind and I'm pretty sure it isn't, then maybe throw a closing '}' before the "else..." and unindent stuff below that or some such thing.

    Hmmm...

    What's the name of the reverse operation of "indent" ?

    Is it really "unindent" ?

    Why not "undent" ? (Shorter, more elegant.)

    Or, better yet "outdent" ? (Logically consistent: "out" is opposite of "in," right?)


    English is a lot more complicated and less precise than Java, right?

    I mean, sometimes I feel as if I am in a maze of twisty little passages, all alike...



    Cheers!

    Z
    Last edited by Zaphod_b; October 5th, 2012 at 06:22 PM.

Similar Threads

  1. dunno how to test this code so i dont know if it works help please
    By jonathanfox in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 24th, 2012, 04:43 AM
  2. how operator( && or || works).....
    By ashish12169 in forum Object Oriented Programming
    Replies: 5
    Last Post: July 20th, 2012, 12:36 AM
  3. I have no idea how this works.
    By GeneralPihota in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 29th, 2011, 09:55 PM
  4. GUI Only Works Sometimes
    By bgd223 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 12th, 2011, 08:08 AM
  5. [SOLVED] Can someone verify if this code for deleting a BST works?
    By scottb80 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 2nd, 2010, 10:19 AM