gpt4 book ai didi

java - 无法对基本类型 int 调用 equals(String)

转载 作者:行者123 更新时间:2023-12-01 13:10:31 25 4
gpt4 key购买 nike

我有一个包含航类号的文本文件,我的目标是让此方法搜索该文本文件,并打印出包含“航类号”的所有行。

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

关于java - 无法对基本类型 int 调用 equals(String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22903275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com