简体   繁体   English

无法在原始类型int上调用equals(String)

[英]Cannot invoke equals(String) on the primitive type int

I have a text file with flight numbers, my goal is to have this method search that text file, and print out all the lines with said "flight number". 我有一个包含航班号的文本文件,我的目标是让此方法搜索该文本文件,并打印出所有带有“航班号”的行。

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

     }
}

Use Integer.parseInt to convert it to an int value, then compare the two int values using == 使用Integer.parseInt将其转换为int值,然后使用==比较两个int值

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

通常没有针对航班号执行的算术运算,因此应为String数组。

fl_number is a int array and search_flight is a String, you should parse it to int to work with fl_number fl_number是一个int数组,而search_flight是一个String,您应该将其解析为int以使用fl_number

So, fl_number[i].equals(search_flight) 因此,fl_number [i] .equals(search_flight)

-> fl_number[i] == Integer.Parse(search_flight) -> fl_number [i] == Integer.Parse(search_flight)

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

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