0

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.

Johnny Mopp
  • 10,608
  • 4
  • 33
  • 58
  • 2
    2 Things: In java, compare strings with `.equals()`. And [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/a/13102066) – Johnny Mopp Oct 15 '20 at 16:57

1 Answers1

-1

For displaying menu until the user input correct data you should use the loop, then you need to read a string and validate it with what type of string you want like LPL or BOH. After the menu, pass the same string to switch statement and write separate switch cases for each menu items. Use separate class to write logic for each case and just call the method from switch case.

Check the below code:
---------------------
static void menu() {
Scanner s = new Scanner(System.in);
String st = "";
while(st!=5){
  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("Enter your choice : ");
  st = s.next();    //Validate for proper string you want

  switch(st){
  case 1: 
         System.out.println("Input Airport Details : ");
  //read data and apply business logic and store data -- do your stuff here

  case 2:
         System.out.println("Input flight details ");
         //read data and apply logic for your operation
 
  case 3:
         System.out.println("enter price plan and calculate profit");
        // here you need to read price plan and write code for calculating profit
  case 4://code to clear data
  
  case 5:
         break;  // to exit
   }
  }
 }

//this code will reinterate through your menu items based on your logic it will perform. Hope you find this helpful.

Praveen L
  • 9
  • 3