1

I am making a program which helps the user calculate the likely profitablilty of running a flight from UK airport to International airport. Uk airport can either be LPL or BOH.

Now, I want to read data from the csv text file and compare it with user input to check if the international airport the user wants matches same international airport in csv file. How do I do it.

Text file Contents

JFK,John F Kennedy International
ORY,Paris-Orly
MAD,Adolfo Suarez Madrid-Baranjas
AMS,Amsterdam Schipol
CAI,Cairo International

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();

        if (ukCode == "LPL" || ukCode == "BOH") {
        
         //where I want to read csv text file and compare user input
       }
     }
   }
 }
Hulk
  • 6,399
  • 1
  • 30
  • 52
  • 1
    Please turn to the [help] to learn how/what to ask here. Just dropping requirements "this is what I want" isn't appreciated. When you try something yourself, and you get stuck with a specific problem, we will gladly help. But please understand that this place is not intended to give guidance with the possibly many steps required to get you from your vision to a working program. What I mean is: yes, you wrote some code (that might also just be the assignment template code you got for your assignment). But then you stop. You didnt tell us what aspect of the assignment gives you problems?! – GhostCat Oct 16 '20 at 07:18
  • 1
    And honestly: I do not understand why people upvoted this request. – GhostCat Oct 16 '20 at 07:19
  • 1
    Finally: `ukCode == "LPL"` ... please read https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java ... you see, before you start comparing values read from files, you have to first understand how to compare values correctly. – GhostCat Oct 16 '20 at 07:20
  • If your csv data is always in a single line, and the data cannot contain the separator (`,` in your case), you can simply use [`String.split()`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#split(java.lang.String)). Otherwise, it might be worth looking for a csv parser library, but that is probably not the point here, as this looks like a homework example. – Hulk Oct 16 '20 at 07:45

1 Answers1

1

In java Strings are reference objects rather than primitives. This means the value of a string is a reference (also known as reference by name). For primitives the reference of the instance is a value... but otherwise Java is a language where object instances are "referenced by name".

Because of this, comparing a string to a value in a .csv requires you use ukCode.equals("LPL") and not == for comparison (as this will compare the reference values).

Now... when you want to "lookup" a value supplied as being LPL or BOH, you'll want to store valid values in a Set (e.g. HashSet) so that you can do a fast comparison. If you iterate over a List (ArrayList for example) the process will be considerably slower (O(n) - access speed is proportional to the number of elements in the list - the speed of access is "linear").

The reason the Set will be faster is because the values of a HashSet are stored at an address/location equivalent to their hashCode (make sure you're aware of the relationship between .equals()/.hashCode() so you don't end up with strange errors). Because elements of a HashSet are stored at a hashed address location that is directly computable, checking for the existence of an element within it is fast (O(1) - "constant" time).

Once you have valid values in a Set, the solution will scale to many values without a degradation in performance.

Rob Evans
  • 2,822
  • 1
  • 9
  • 15