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

Thread: Beginner Java program help

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

    Default Beginner Java program help

    Hi guys,
    I started learning java recently from programmingbydoing.com. I'm on lesson 33, where I have to make a short adventure game that asks you where you want to go from two presented options, and prints out a response. First of all, the nested if statements confused me, but besides that, I can't get the program to print out the correct response. It just skips to the else and exits. Here is my code so far:

    import java.util.Scanner;
    public class shortGame
    {
    public static void main (String [] args)
    	{
    Scanner keyboard = new Scanner (System.in);
     
    String answer;
     
     
     
    System.out.println ("Welcome to this Crappy Game");
     
    System.out.print ("You are in a creepy house. Would you like to go upstairs, or into the kitchen? ");
    answer = keyboard.next();
    if (answer == "upstairs")
    		{
     
    			{
    	System.out.println ("Upstairs you see a hallway. At the end of the hallway is a bedroom. there is also a bathroom. where would you like to go? ");
    	answer = keyboard.next();
    			}
     
    	if (answer == "bedroom")
    			{
    				{
    		System.out.print ("You are in a plush bedroom with expensive looking hardwood furniture. The bed is unmade. In the back of the room, the closet door is open. Would you like to go in?");
    		answer = keyboard.next();
    				}
    	 if (answer == "no")
    				{
    		System.out.print ("You never found out what was in there. thanks for playing this crappy piece of shit Game");
    				}
    	 if (answer == "yes")
    				{
    		System.out.print ("There were clothes. that's about it, thanks for playing this crappy piece of shit Game");
     
    				}
    			};
     
     
    	 if (answer == "bathroom")
    			{
    	System.out.print ("well, this is stupid, so I'm ending it here. thanks for playing this Game");
    			}			
     
    		}	
    else
    		{
    System.out.print ("OK");
    		}
    	}
    }


    I finally got rid of all my errors and the code now compiles, but I think the string may not be updating when the user types the input. Its funny, I got it to work easily with Int in an earlier lesson.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Beginner Java program help

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Do not use '==' to compare String objects. Use the equals() method instead:

    if ( this.equals( that ) )

    where this and that are both String objects.

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

    Default Re: Beginner Java program help

    Well thank you for the graciuos welcoming and quick reply!
    I don't think I'm implimenting this properly. You're saying that if(answer =="blank") is incorrect, am I right? I changed that line now to if(String.answer("blank")) but now it won't compile. Can you show me where I'm going wrong?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Beginner Java program help

    It would be:

    if ( answer.equals( "blank" ) ) {}

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

    Default Re: Beginner Java program help

    ah, thank you so much, that worked, now I can finish the program. I have a feeling I'll be posting here a lot more haha. Cheers, friend

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Beginner Java program help

    You're welcome. Come back anytime.

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Location
    British Columbia, Canada
    Posts
    6
    My Mood
    Busy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java program help

    Even though the good people above have helped you solve your problem, I'd like you some advice:
    1. Always start class names with an Uppercase letter.
    2. Use switch statements for primitive types including strings.

    import java.util.Scanner;
     
    /**
     * By convention, class names start with an Uppercase letter.
     * Also, using the eclipse IDE, you can do Ctrl+Shift+f to format (clean) your code automatically.
     */
    public class ShortGame {
     
    	public static void main (String [] args) {
    	    Scanner keyboard = new Scanner (System.in);
    		String answer;
    		System.out.println ("Welcome to this Crappy Game\nYou are in a creepy house. Would you like to go upstairs, or into the kitchen?");
    		answer = keyboard.next();
    		switch(keyboard.next()) {
    		case "upstairs":
    		    System.out.println ("Upstairs you see a hallway. At the end of the hallway is a bedroom. there is also a bathroom. where would you like to go? ");
    			switch(keyboard.next()) {
    			    /**
    				 * Any cases can be placed here.
    				 */
    			}
    			break;
    		case "upstairs":
    		    /**
    			 * Place function here.
    			 */
    	        break;
    		default:
    		    // What to do if the command did not equal any of the cases above, or if a case did not "break" out of the switch statement.
    		    System.out.println("Invalid command.");
    		}
    	}
     
    }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Beginner Java program help

    Use switch statements for primitive types including strings.
    String objects are not primitives, though Java allows the programmer to sometimes treat them as if they were.

    Some disagree with your affection for the switch() statement. A programmer I respect once said, "The switch() statement is for the lazy and unimaginative." While it's a tool for use, there are often better tools available - a personal choice.

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Location
    British Columbia, Canada
    Posts
    6
    My Mood
    Busy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java program help

    Quote Originally Posted by GregBrannon View Post
    String objects are not primitives, though Java allows the programmer to sometimes treat them as if they were.

    Some disagree with your affection for the switch() statement. A programmer I respect once said, "The switch() statement is for the lazy and unimaginative." While it's a tool for use, there are often better tools available - a personal choice.
    Explain on the "tools" you are referring to? Switch statements make your code far cleaner than and can become a good practice.

    Example:

    if (obj.getString().equals("blah blah blah)) {}
    else if (obj.getString().equals("blah blah blah)) {}
    else if (obj.getString().equals("blah blah blah)) {}
    else if (obj.getString().equals("blah blah blah)) {}
    else if (obj.getString().equals("blah blah blah)) {}

    ^ Your retrieving the string every time from the var obj, therefore more of a memory heap and more work for the GC. Even though you could create another var that refers to the obj.getString() (String s = obj.getString()), this is what I tend to see in most applications programmed by other intermediate programmers.

Similar Threads

  1. beginner java program help
    By Peeboelmeebo in forum Object Oriented Programming
    Replies: 5
    Last Post: February 13th, 2013, 07:24 PM
  2. Beginner,first java program assignment
    By geezlouise in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 31st, 2013, 07:36 PM
  3. Replies: 2
    Last Post: January 18th, 2013, 10:12 AM
  4. Java program (beginner) help?!
    By rk2010 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 10th, 2012, 12:30 PM
  5. Beginner Java Program Help/ txt database search
    By JavaConfused in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 21st, 2011, 09:20 AM