0

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

Johnny Mopp
  • 10,608
  • 4
  • 33
  • 58
  • 3
    Use `.equals` instead of `==` to compare `String`s. – iota Dec 29 '20 at 19:39
  • Thank you but program still skips if else if and goes to else straight away?? –  Dec 29 '20 at 19:43
  • Do the file contents have the commas? Also, I would check if there is a new line character in the strings while either reading from the file or from the STD_INPUT Scanner. Print out the string lengths and see if they match. – Sir Barksalot Dec 29 '20 at 19:46
  • @SirBarksalot The file contents contains commas and each content is on a new line. Also what would printing out the string lengths do if I may ask? –  Dec 29 '20 at 19:48
  • Are you using an IDE to debug? You really should if not. Will save you a lot of time – OldProgrammer Dec 29 '20 at 19:48
  • *FYI:* Ending a loop with a `continue` statement is very redundant. Remove it. – Andreas Dec 29 '20 at 19:50
  • Don't create multiple `Scanner` objects wrapping `System.in`. Only ever create one of them. Get rid of `sc`. – Andreas Dec 29 '20 at 19:51
  • It would show if there was a new line character hiding somewhere but I misunderstood your question. You're saying the program never waits at `String overseasCode = sc.nextLine();`? – Sir Barksalot Dec 29 '20 at 19:56
  • Yeah, you probably have a dangling new line character in your OS's standard input after entering `String ukCode = sc.nextLine();` – Sir Barksalot Dec 29 '20 at 20:04
  • @SirBarksalot Sorry if I was not clear enough..The program asks for the international code but then instead of printing the full airport name, it prints out "International airport code not valid...returning to menu" –  Dec 29 '20 at 20:08
  • Okay, then I really think you should do what @iota said and replace == with .equals for String comparisons. – Sir Barksalot Dec 29 '20 at 20:18
  • Also, I would check that you're reading the comma characters from the text file – Sir Barksalot Dec 29 '20 at 20:20
  • @SirBarksalot how would I split at commas? –  Dec 29 '20 at 20:21
  • Change `airportList.add(scan.nextLine());` to `airportList.add(scan.nextLine().replace(",", "").trim());` to remove commas and needless whitespaces. And change == to .equals for String. – Sir Barksalot Dec 29 '20 at 20:37

0 Answers0