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

Thread: Help with if statement that wont act right...

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

    Default Help with if statement that wont act right...

    When I type in yes it come out with all the text in the yes if statement plus TEXT4(the last if statement). Can some one spot the error? I also tried if ( ! (answer.equals(All)) ) it still comes out with TEXT4....

    public static void main(String args[]){
    		String answer1;
    		String yes = "yes";
    		String no = "no";
    		String[] All = {"yes", "no"};
    		Scanner answer = new Scanner(System.in);
     
    		System.out.println("TEXT1");
    		System.out.println("yes/no");
     
    		answer1 = answer.nextLine();
     
    		answer1.toLowerCase();
     
     
     
     
    		if(answer1.equals(yes)){
    			System.out.println("TEXT2");
    		} 
     
    		if(answer1.equals(no)){
    			System.out.println("TEXT3");
    			System.out.println("yes/no");
     
    		}
     
    		if (!answer.equals(yes)&&!answer.equals(no)){
    			System.out.println("TEXT4");
     
     
    		}
     
     
    		}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with if statement that wont act right...

    Strings are immutable. Methods such as toLowerCase() do not actually change the String, they return a new String with the transform applied.

    To prove this, print out the value of answer1 after you call the toLowerCase() method.

    Either save the value returned from the toLowerCase() method, or look at the String API for a function that will help you check for equality while ignoring case.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    deathpk (October 10th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with if statement that wont act right...

    HAHAHAHHAHA wow im dumb... I had the wrong variable in it... My bad fixed it.

    Thank you kevin for that I will fix that as well.

    Closed

Similar Threads

  1. Timer wont stop...
    By pottsiex5 in forum AWT / Java Swing
    Replies: 8
    Last Post: October 7th, 2011, 02:49 PM
  2. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 04:40 PM
  3. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM
  4. Compliing fine but wont run--please help!
    By Nova in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2010, 07:51 PM
  5. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM