简体   繁体   中英

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 ==

          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

So, fl_number[i].equals(search_flight)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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