0

It's my first assignment in my Java class and I'm already frustrated lol. Brand new rookie here.

I want to make it so I can input LAX or SAN or LAS and it will move on and list the other statements under my if statement. Instead, it always goes straight to what is under the else statement, even if I do input "LAX" in the console for example.

Here is what I have:

import java.util.Scanner;

public class MilesCalculator {

    public static void main(String[] args) {
        
        String airCode = "";
        
        System.out.println("\t\t\tMiles Calculator\n");
        
        System.out.println("This program will calculate how many miles you earn on your Phoenix Air Flight.\n");
        System.out.println("DESTINATIONS");
        System.out.println("Los Angeles (LAX)");
        System.out.println("San Diego (SAN)");
        System.out.println("Las Vegas (LAS)\n");
        
        Scanner scnr = new Scanner(System.in);
        
        System.out.print("Please enter the destination airport code: ");
        
        airCode = scnr.nextLine();
        
        if (airCode == "LAX" || airCode == "SAN" || airCode == "LAS") {
            System.out.println("STATUS");
            System.out.println("1. Bronze");
            System.out.println("2. Silver");
            System.out.println("3. Gold");
            System.out.println("4. Platinum\n");
            
            System.out.print("Please enter your frequent flyer status option (1-4): ");

        }
            else {
                System.out.printf("Phoenix Air does not fly to %s. There will be zero miles earned. \nPress enter to quit...", airCode);
                System.exit(0);
                scnr.nextLine();
        }
Balastrong
  • 4,336
  • 2
  • 12
  • 31

2 Answers2

0

When comparing strings in Java, you can't use ==. Instead you must use equals or equalsIgnoreCase, like this:

if (airCode.equals("LAX") || airCode.equals("SAN") || airCode.equals("LAS")) {

There are lots of explanations of why, but in your very early days of coding I'd say "just accept it". For now. Later on you must understand why.

Sam
  • 5,424
  • 1
  • 18
  • 33
0

When comparing Strings do not use (==) //it's wrong practice but instead use the String.equals() function Or the String.equalsIgnoreCase() function then specify what to compare in the parameter

E.g if(airCode.eqauls("LAX") || air code.equals("SAN") || airCode.equals("LAS")