无法在基本类型int上调用equals(String) [英] Cannot invoke equals(String) on the primitive type int

查看:1001
本文介绍了无法在基本类型int上调用equals(String)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

     }
}


推荐答案

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

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

这篇关于无法在基本类型int上调用equals(String)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆