我有一个带有航班号的文本文件,我的目标是让此方法搜索该文本文件,并打印出所有带有"航班号"的行。
public static void print_flight(int rcount,int[]reservation_code,int[]fl_number,String[]last_name,String[]first_name,String[]seat_type,double[]seat_cost)
{
int i, total=0;
String search_flight = "";
String output = "Enter the Flight Number you are searching for";
search_flight = JOptionPane.showInputDialog(null,
output, " ",
JOptionPane.QUESTION_MESSAGE);
for (i = 0; i <=rcount; ++i) {
//CHECK flight number
if(fl_number[i].equals(search_flight))//ERROR IS HERE
{
total+=fl_number[i]; //not sure if that is right
System.out.println(reservation_code[i]+" "+fl_number[i]+" "+last_name[i]+" "+first_name[i]+" "+seat_type[i]+" "+seat_cost[i]);
}
}
}
使用
Integer.parseInt 将其转换为 int 值,然后使用 ==
比较两个 int 值
int flight_number = Integer.parseInt(search_flight); //convert to int
if(fl_number[i] == flight_number)//compare two ints
通常没有对航班号执行的算术,所以东西应该是字符串数组。
fl_number 是一个 int 数组和search_flight是一个字符串,你应该把它解析为 int 才能使用fl_number
所以,fl_number[i].equals(search_flight)
-> fl_number[i] == Integer.Parse(search_flight)