Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: why do if statements hate me

  1. #1
    Zeezj's Avatar
    Zeezj is offline Member
    Join Date
    Mar 2011
    Location
    Cambridge, MA
    Posts
    11
    Rep Power
    0

    Angry why do if statements hate me

    hello people. see, what i'm doing is trying to write a simple text-based adventure, which you can probably imagine involves a ton of if statements. but, for some reason java has decided that my if statements are no good here and completely ignores them (by that i mean that when i answer the question in the program, it just skips over the thing the if statement is supposed to do.). what i want to know is how i can make them work. also, if there is a way to get if/else statements to work that would be great too.

    here's the code:
    Java Code:
    import java.util.Scanner;
    public class adventure {
    	public static void main(String args[]) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.print("enter your name");
    		System.out.println();
    		String name = scanner.nextLine();
    		System.out.print("Welcome to Text Based Adventure, " + name + "! Text Based Adventure takes place in a world of mediocrity, a world of decidedly non-wonder! the world of... well, the same world you live in, actually.");
    		System.out.println("would you like some coffee?[YES/NO]");
    		System.out.println();
    		String yn1 = scanner.nextLine();
    		if (yn1 == "yes") {
    			System.out.println("sorry, I don't actually have any, you'll have to make it yourself.");
    		}
    		if (yn1 == "no") {
    			System.out.println("oh. well, could you get me some?");
    		}
    		System.out.println("there is a coffee maker to the WEST. to get to it, type 'go west' or 'go coffee maker'");
    		String cmd1 = scanner.nextLine();
    		if (cmd1 == "go west" & cmd1 == "go coffee maker") { System.out.println("you walk over to the coffee maker. to make coffee, type 'use coffee maker'"); }
    	}
    
    }
    p.s. it is unfinished, as you can probably see.
    from the desk of August Underhill-Proulx, CEO of Awesomecorp ltd.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: why do if statements hate me

    Your if statements are faulty in that you should not compare Strings with ==. Use the equals or equalsIgnoreCase methods instead. The reason that this is important is that when you use ==, you check to see if both variables refer to the same object, and you really don't care about this. You want to know instead if both String variables hold the same characters in the same order, and that's what equals(...) does.

  3. #3
    Zeezj's Avatar
    Zeezj is offline Member
    Join Date
    Mar 2011
    Location
    Cambridge, MA
    Posts
    11
    Rep Power
    0

    Default Re: why do if statements hate me

    how do i do that? sorry if I'm being a noob I'm pretty new to java.
    from the desk of August Underhill-Proulx, CEO of Awesomecorp ltd.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: why do if statements hate me

    Quote Originally Posted by Zeezj View Post
    how do i do that? sorry if I'm being a noob I'm pretty new to java.
    What are you confused about? How do you call a method on a reference variable? You call the equals method on one String and pass in the other String.
    Java Code:
    if (myString.equals("foo")) {
      //...
    }
    
    if (myString.equalsIgnoreCase("bar")) {
      //...
    }
    Zeezj likes this.

  5. #5
    Zeezj's Avatar
    Zeezj is offline Member
    Join Date
    Mar 2011
    Location
    Cambridge, MA
    Posts
    11
    Rep Power
    0

    Default Re: why do if statements hate me

    thanks! that fixed it!
    from the desk of August Underhill-Proulx, CEO of Awesomecorp ltd.

Similar Threads

  1. Help with IF Statements and GUI
    By university123 in forum New To Java
    Replies: 8
    Last Post: 10-14-2010, 04:38 AM
  2. if else if statements
    By Allspark in forum New To Java
    Replies: 9
    Last Post: 09-28-2010, 06:50 PM
  3. Help with if else statements
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 01:25 AM
  4. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  5. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM

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
  •