I'm trying to have user input stored into a string variable and then that string is tested to determine which output to give. I thought this would not be a problem and I cannot understand what is wrong here. If I type in F22 or any of the other options as my user input, it gives me the else statement so clearly it's not storing the value the same way I think that it is. My code is below.
public static void main(String[] args){
aircraft();
}
public static void aircraft(){
//Give info about aircraft of their choice.
airplane Boeing787 = new airplane(); //From here to line 17 I'm just defining variables
airplane F22 = new airplane();
airplane Gulfstream_G650 = new airplane();
Boeing787.passengers = 250;
Boeing787.price = 120000000;
F22.passengers = 2;
F22.price = 138000000;
Gulfstream_G650.passengers = 19;
Gulfstream_G650.price = 65000000; //TO HERE! For variables!
System.out.println("What airplane do you want to know about? \nYou can select either: Boeing787, F22, or Gulstream_G650");
Scanner user_input = new Scanner(System.in);
String plane = user_input.next();
if (plane == "Boeing787"){
System.out.println("This aircraft costs " + Boeing787.price + " and holds up to " + Boeing787.passengers + " people.");
}
else if (plane == "F22"){
System.out.println("This aircraft costs " + F22.price + " and holds up to " + F22.passengers + " people.");
}
else if (plane == "Gulfstream_G650"){
System.out.println("This aircraft costs " + Gulfstream_G650.price + " and holds up to " + Gulfstream_G650.passengers + " people.");
}
else{
System.out.println("You must have spelled it wrong, please try again. Remember to use appropriate Caps.");
aircraft();
}
}
and my airplane class is simply:
public class airplane{
int price;
int passengers;}
I prefer an explanation over a fix. I'm a beginner programmer so I suspect it's not a crazy mistake. My guess is something is wrong either where I take in the input or on the following line where it says "String plane = user_input.next();"