Results 1 to 5 of 5
  1. #1
    SabuLuvu is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Question If statements with Strings

    I'm relatively new with Java and I'm trying to make a text game.
    The code I'm having trouble with

    Java Code:
    		System.out.println("You are sitting in a dark room. Try standing up");
    		Answer[2] = input.nextLine();
    
    		if (Answer[2].equals("Stand") && Answer[2].equals("stand") && Answer[2].equals("Stand up") && Answer[2].equals("stand up"))
    			{
                              System.out.println("Congratulations! You are now standing.");
                             }
    		else {
                             System.out.println("You've gotta stand up first...");
                           }
    		input.close();
    I'm thinking my problem is using && as if I need to satisfy all those conditions, but I only want the option for each. I've no idea how to get the Congrats text without satisfying all conditions except by only using one condition.
    Any help is appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: If statements with Strings

    Your assumption is correct.
    You are testing a single input and saying "in order to do this then this input needs to be equal to 'Stand' and 'stand' and 'Stand up'..."
    Which is clearly impossible.
    You want to use 'or', not #and'...so || not &&.

    I'll move this to New To Java as well, as this is not the correct section.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,597
    Rep Power
    5

    Default Re: If statements with Strings

    Considering the values to which you are comparing you could simplify the comparison somewhat by using the String.equalsIgnoreCase() method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    SabuLuvu is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: If statements with Strings

    Thanks!

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: If statements with Strings

    no but, as suggested using equalsIgnoreCase() will remove the need to do any conversion.

Similar Threads

  1. If Statements or Something Better?
    By collegeconfused in forum New To Java
    Replies: 1
    Last Post: 05-05-2013, 10:28 AM
  2. If statements
    By Joshua.Robinson in forum New To Java
    Replies: 10
    Last Post: 11-15-2012, 09:41 AM
  3. If Statements
    By katymccl in forum New To Java
    Replies: 6
    Last Post: 02-02-2012, 08:06 AM
  4. Help with IF Statements and GUI
    By university123 in forum New To Java
    Replies: 8
    Last Post: 10-14-2010, 04:38 AM
  5. If statements, input, and strings
    By evolvepwnz in forum New To Java
    Replies: 16
    Last Post: 10-22-2009, 09:58 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
  •