I am creating a program that will allow the user to enter a UK airport and an international airport to assess the feasibility of running a flight between them.
I have created a text file that contains the international airport info and read this into an ArrayList
for flexibility.
I want to return the user to the main menu if the user does not enter a valid international airport. I have used switch
statement for the menu choices.
File contents/Code/input/output are as shown below.
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
/*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());
} */
ArrayList<String> airportList = new ArrayList<>();
try (Scanner scan = new Scanner(new FileReader("Airports.txt"))) {
while (scan.hasNext()) {
airportList.add(scan.nextLine().replace(",", "").trim());
}
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<");
System.out.println("\n" + "Enter code for UK airport");
String ukCode = scanner.next();
do {
System.out.println("Enter valid code for overseas airport");
String overseasCode = scanner.next();
if (airportList.contains(overseasCode) && overseasCode.equals("JFK")) {
System.out.println("John F Kennedy International");
break;
} else if (airportList.contains(overseasCode) && overseasCode.equals("ORY")) {
System.out.println("Paris-Orly");
break;
} else if (airportList.contains(overseasCode) && overseasCode.equals("MAD")) {
System.out.println("Adolfo Suarez Madrid-Baranjas");
break;
} else if (airportList.contains(overseasCode) && overseasCode.equals("AMS")) {
System.out.println("Amsterdam Schipol");
break;
} else if (airportList.contains(overseasCode) && overseasCode.equals("CAI")) {
System.out.println("Cairo International");
break;
} else { // ***
System.out.println("Not valid input");
}
continue; // *** where I want to return user to main menu**
} while (ukCode.equals("LPL") || ukCode.equals("BOH"));
}
}
}
Input:
------------------------------------------------------
| Enter 1 to input airport details |
| Enter 2 to input flight details |
| Enter 3 to enter price plan and calculate profit |
| Enter 4 to clear data |
| Enter 5 to quit |
------------------------------------------------------
[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]
Enter menu choice:
1
You selected >Enter airport details<
Enter code for UK airport
LPL
Enter valid code for overseas airport
GHGH **(not valid - doesnt exist in text file)**
Not valid input
Enter valid code for overseas airport //**instead of this i want user to return to main menu**
try it this way:
...
...
break; // where I want to return user to main menu
} while (ukCode.equals("LPL") || ukCode.equals("BOH"));
menu();
This would make your do-while loop absolutely futile, but you would get what you want. Look into the documentation about loops to enhance your code... https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.