I am making a program to assess the feasibility of running a flight from a UK airport to an International airport.
- I have created a text file which I have read into an ArrayList so that I can get values needed. I need to now ask the user to enter the UK airport (LPL or BOH) and then if it is LPL or BOH, then the program proceeds to ask the user for the overseas airport code and check if it is valid.
I have done the UK airport part but strangely, when the user enters LPL or BOH, the program does not ask the user for the Overseas airport and instead prints the else statement code.. If the international code really does not exist in arraylist, then the program should return user to menu again.
I have provided file contents, code, input and output..thank you very much btw, I think it is a very basic mistake which I cannot grasp.
File Contents:
JFK,
John F Kennedy International,
5326,
5486,
ORY,
Paris-Orly,
629,
379,
MAD,
Adolfo Suarez Madrid-Baranjas,
1428,
1151,
AMS,
Amsterdam Schipol,
526,
489,
CAI,
Cairo International,
3779,
3584
CODE:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.io.IOException;
class Main {
// menu method
static void menu() {
System.out.println("------------------------------------------------------");
System.out.println("| Enter 1 to input airport details |");
System.out.println("| Enter 2 to input flight details |");
System.out.println("| Enter 3 to enter price plan and calculate profit |");
System.out.println("| Enter 4 to clear data |");
System.out.println("| Enter 5 to quit |");
System.out.println("------------------------------------------------------");
}
public static void main(String[] args) throws IOException {
System.out.println("Welcome...");
System.out.println();
menu();
System.out.println();
// reading file-- ignore comment
/*File file = new File("Airports.txt");
Scanner fileScan = new Scanner(file);
System.out.println(fileScan.nextLine());
//getting all lines from text file
while(fileScan.hasNextLine()) {
System.out.println(fileScan.nextLine());
} */
//reading file into ArrayList for flexibility
ArrayList<String> airportList = new ArrayList<>();
try (Scanner scan = new Scanner(new FileReader("Airports.txt"))) {
while (scan.hasNext()) {
airportList.add(scan.nextLine());
}
System.out.println(airportList);
}
Scanner scanner = new Scanner(System.in);
System.out.println("\n" + "Enter menu choice: ");
int menuChoice = scanner.nextInt();
switch (menuChoice) {
case 5:
System.out.println("You selected >Quit<. Program ending...");
break;
case 1:
System.out.println("You selected >Enter airport details<");
Scanner sc = new Scanner(System.in);
System.out.println("Enter code for UK airport");
String ukCode = sc.nextLine();
while (ukCode.equals("LPL") || ukCode.equals("BOH")) {
**System.out.println("Enter valid code for overseas airport");
String overseasCode = sc.nextLine();** // **doesnt ask for this**
if (airportList.contains(overseasCode) && overseasCode == "JFK") {
System.out.println("John F Kennedy International");
} else if (airportList.contains(overseasCode) && overseasCode == "ORY") {
System.out.println("Paris-Orly");
} else if (airportList.contains(overseasCode) && overseasCode == "MAD") {
System.out.println("Adolfo Suarez Madrid-Baranjas");
} else if (airportList.contains(overseasCode) && overseasCode == "AMS") {
System.out.println("Amsterdam Schipol");
} else if (airportList.contains(overseasCode) && overseasCode == "CAI") {
System.out.println("Cairo International");
}
else {
System.out.println("International airport code not valid...returning to menu");
}
continue;
}
break;
}
}
}
INPUT/OUTPUT:
Enter menu choice:
1
You selected >Enter airport details<
LPL
Enter valid code for overseas airport
ORY
International airport code not valid...returning to menu
Enter valid code for overseas airport
Thank you for the help PS: Sorry for the lengthy explanation