Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
A very simple Java question involving Strings and if else statements.?
Hello there. I am a new java programmer and am learning about all the features. So my question is, is how can I add Strings into if else statements? I know how to do it with integers, but not strings.
Example with integers.
import java.util.Scanner;
public class yahooTest {
public static void main(String args []) {
Scanner kb = new Scanner(System.in);
int yes, no.
yes = 1;
no - 0;
System.out.println("Yes or No?");
Scanner kb = answer.nextInt();
if (yes == 1) {
System.out.println("You've chosen yes.");
}else{
System.out.println("You've chosen no.");
}
}
}
Would someone please help me out by telling me how I can do this with Strings? I am trying to make a choose your own adventure game, and it would be very very helpful to know. Thank you very much!
1 Answer
- brilliant_movesLv 75 years agoFavorite Answer
Hi, Finn. This is how you would use if / else with a String. You could use .equals() instead of .equalsIgnoreCase() if you want.
import java.util.Scanner;
public class YahooTest {
public static void main(String args []) {
Scanner kb = new Scanner(System.in);
String answer;
System.out.println("Yes or No?");
answer = kb.nextLine();
if (answer.equalsIgnoreCase("yes")) {
System.out.println("You've chosen yes.");
}else{
System.out.println("You've chosen no.");
}
}
}