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

Thread: New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!

    No matter what I input the program always returns "What!!!". Any help is appreciated. Thanks!

     
    import java.util.Scanner;
     
    class IfElseDemo {
        public static void main(String[] args) {
     
            String status;
     
     
            Scanner keyboard = new Scanner(System.in);
    		    System.out.println("Please enter Eat or Drink");
            status = keyboard.nextLine();
     
     
            if (status == "Eat") {
                System.out.println("Yum!!!");
            } else if (status == "Drink") {
                System.out.println("Wow!!!");
            } else {
    			System.out.println("What???");
     
            }
     
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: New at Java... my if, else if, else program doesn't seem to work, skips to else.H


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

    KevinE (November 1st, 2010)

  4. #3
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: New at Java... my if, else if, else program doesn't seem to work, skips to else.H

    try if(status.equals("Eat"))

  5. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New at Java... my if, else if, else program doesn't seem to work, skips to else.H

    Just as I submitted this I tried changing the status ==

     
    class IfElseDemo {
        public static void main(String[] args) {
     
            String status;
     
     
            Scanner keyboard = new Scanner(System.in);
    		    System.out.println("Please enter Eat or Drink");
            status = keyboard.nextLine();
     
     
            if (status.equals("Eat")) {
                System.out.println("Yum!!!");
            } else if (status.equals("Drink")) {
                System.out.println("Wow!!!");
            } else {
    			System.out.println("What???");
     
            }
     
        }
    }

  6. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New at Java... my if, else if, else program doesn't seem to work, skips to else.H

    Thanks for the quick replies glad that I could figure it out on my own! Just started an Intro to Java course... sure I'll be back.

Similar Threads

  1. Applet paint program frame work.
    By ShaunB in forum Java Swing Tutorials
    Replies: 4
    Last Post: October 31st, 2012, 08:44 AM
  2. [SOLVED] Can't get JFreeChart to work
    By igniteflow in forum Java SE APIs
    Replies: 2
    Last Post: February 15th, 2011, 02:19 AM
  3. Out of memory work around for a java application (please help!)
    By javameanslife in forum Java Theory & Questions
    Replies: 5
    Last Post: January 22nd, 2010, 04:27 AM
  4. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM