英文:
How do I read csv text file so I can compare specific values from csv file with user input?
问题
文本文件内容:
JFK,约翰·肯尼迪国际机场
ORY,巴黎-奥利机场
MAD,阿多尔福·苏亚雷斯·马德里-巴拉哈斯机场
AMS,阿姆斯特丹史基浦机场
CAI,开罗国际机场
代码部分:
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 {
// 菜单方法
static void menu() {
System.out.println("------------------------------------------------------");
System.out.println("| 输入 1 输入机场详情 |");
System.out.println("| 输入 2 输入航班详情 |");
System.out.println("| 输入 3 输入价格计划并计算利润 |");
System.out.println("| 输入 4 清除数据 |");
System.out.println("| 输入 5 退出 |");
System.out.println("------------------------------------------------------");
}
// 文本文件
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();
// 主代码
Scanner scanner = new Scanner(System.in);
System.out.println("\n" + "欢迎");
menu();
int menuChoice = scanner.nextInt();
if (menuChoice == 5) {
System.out.println("\n" + "您选择了退出");
System.out.println("程序结束。");
} else if (menuChoice == 1) {
System.out.println("\n" + "您选择了输入机场详情");
System.out.println("\n" + "请输入英国机场的三字代码");
String ukCode = scanner.next();
if (ukCode.equals("LPL") || ukCode.equals("BOH")) {
// 在这里我想从CSV文本文件中读取并比较用户输入
}
}
}
}
英文:
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
}
}
}
}
答案1
得分: 1
在Java中,字符串(Strings)是引用对象(reference objects),而不是基本数据类型。这意味着字符串的值是一个引用(也称为按名称引用)。对于基本数据类型,实例的引用是一个值... 但除此之外,Java是一种对象实例是“按名称引用”的语言。
由于这个原因,在将字符串与.csv
中的值进行比较时,您需要使用ukCode.equals("LPL")
,而不是使用==
进行比较(因为这将比较引用值)。
现在... 当您想要查找一个作为LPL或BOH的值时,您将希望将有效值存储在一个Set
(例如HashSet
)中,以便您可以进行快速比较。如果您遍历一个List
(例如ArrayList
),该过程将会相对较慢(O(n)
- 访问速度与列表中元素的数量成比例 - 访问速度是“线性的”)。
Set之所以更快,是因为HashSet
的值存储在与它们的hashCode等效的地址/位置上(确保您了解.equals()
/.hashCode()
之间的关系,以免出现奇怪的错误)。因为HashSet
的元素存储在可直接计算的散列地址位置上,所以在其中检查元素的存在性是快速的(O(1)
- “常数”时间)。
一旦您在Set中有了有效的值,解决方案将在许多值的情况下保持性能不降。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论