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!
Code Java:
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???");
}
}
}
Re: New at Java... my if, else if, else program doesn't seem to work, skips to else.H
Re: New at Java... my if, else if, else program doesn't seem to work, skips to else.H
try if(status.equals("Eat"))
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 ==
Code Java:
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???");
}
}
}
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.