简体   繁体   English

如何读取 csv 文本文件,以便将 csv 文件中的特定值与用户输入进行比较?

[英]How do I read csv text file so I can compare specific values from csv file with user input?

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.英国机场可以是 LPL 或 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.现在,我想从 csv 文本文件中读取数据并将其与用户输入进行比较,以检查用户想要的国际机场是否与 csv 文件中的同一个国际机场相匹配。 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
       }
     }
   }
 }

In java Strings are reference objects rather than primitives.在 java 中,字符串是引用对象而不是基元。 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".对于原语,实例的引用是一个值……但除此之外,Java 是一种“按名称引用”对象实例的语言。

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).因此,将字符串与.csv的值进行比较需要您使用ukCode.equals("LPL")而不是==进行比较(因为这将比较参考值)。

Now... when you want to "lookup" a value supplied as being LPL or BOH, you'll want to store valid values in a Set (eg HashSet ) so that you can do a fast comparison.现在...当您想“查找”作为 LPL 或 BOH 提供的值时,您需要将有效值存储在Set (例如HashSet )中,以便您可以进行快速比较。 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").如果您迭代一个List (例如ArrayList ),该过程将显着变慢( O(n) - 访问速度与列表中的元素数量成正比 - 访问速度是“线性”的)。

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). Set 更快的原因是因为HashSet的值存储在与它们的 hashCode 等效的地址/位置(确保您了解.equals() / .hashCode()之间的关系,因此您不会最终出现奇怪的错误)。 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).由于HashSet元素存储在可直接计算的散列地址位置,因此检查其中的元素是否存在很快O(1) - “常数”时间)。

Once you have valid values in a Set, the solution will scale to many values without a degradation in performance.一旦您在 Set 中有有效值,该解决方案将扩展到许多值而不会降低性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM