I am making a program which will calculate likely profitability of running a flight between UK airport and overseas airport.
In one part of my code, I want to compare user input to String, if user input is not valid then the program should return user to menu and the user should be able to input How do I do it?
I will highlight the code that I have wrote for this but I am not sure what is going wrong.
Code:
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
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("------------------------------------------------------");
}
// text file
public static void main(String[] argv) throws Exception {
BufferedReader myFile = new BufferedReader(new FileReader("Airports.txt"));
ArrayList<String> listOfLines = new ArrayList<>();
String line = myFile.readLine();
while (line != null) {
listOfLines.add(line);
line = myFile.readLine();
}
myFile.close();
// main code
Scanner scanner = new Scanner(System.in);
System.out.println("\n" + "Welcome");
menu();
int menuChoice = scanner.nextInt();
if (menuChoice == 5) {
System.out.println("\n" + "You have selected quit");
System.out.println("Program ending.");
} else if (menuChoice == 1) {
System.out.println("\n" + "You have selected input airport details");
System.out.println("\n" + "Enter 3 letter airport code for UK airport");
String ukCode = scanner.next();
// need help here below
**if (ukCode != "LPL" || ukCode != "BOH") {
menu();
menuChoice = scanner.nextInt();**
// above bold, I want program to keep displaying menu and making user enter data again if they have not inputted the correct 3 digit code such as "LPL" or "BOH".
}
}
}
}
Output of my code: If user inputs 3 digit code incorrectly:
Welcome ------------------------------------------------------ | 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 | ------------------------------------------------------ 1 You have selected input airport details Enter 3 letter airport code for UK airport rfrw ------------------------------------------------------ | 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 | ------------------------------------------------------ 1 // doesn't do anything after 1 is inputted for the 2nd time.