无法在基本类型int上调用equals(String)

时间:2014-04-07 03:47:51

标签: java string parsing methods integer

我有一个带有航班号的文本文件,我的目标是让这个方法搜索该文本文件,然后用#34;航班号"打印出所有行。

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]);
             }

     }
}

3 个答案:

答案 0 :(得分:1)

使用Integer.parseInt将其转换为int值,然后使用==

比较两个int值
          int flight_number = Integer.parseInt(search_flight); //convert to int
          if(fl_number[i] == flight_number)//compare two ints

答案 1 :(得分:0)

通常没有对航班号执行算术,所以事情应该是字符串数组。

答案 2 :(得分:0)

fl_number是一个int数组 和 search_flight是一个String,您应该将其解析为int以使用fl_number

所以,fl_number [i] .equals(search_flight)

- &GT; fl_number [i] == Integer.Parse(search_flight)