0

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();"

Brandon Willis
  • 145
  • 2
  • 15
  • I looked online for about 20 minutes before posting the question. Perhaps I just didn't know exactly what to ask, but I'm not someone who just plugs any question I have into SO. Where is this question elsewhere anyways? I'd like to know what I didn't type in when looking for this. – Brandon Willis Sep 08 '15 at 23:14

2 Answers2

2

It is because you have to compare Strings with .equals function, not with ==.

if(plane.equals("F22")){
 //Code
}
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
1

Because you are using the incorrect equals method. You need to use the .equals() method associated with Objects. You are currently using the == method used to evaluate equality of primitives.

Change all your conditionals and you should see a more pleasing result.

andrewdleach
  • 2,458
  • 2
  • 17
  • 25