2014-04-07 25 views
0

我有一個帶有航班號的文本文件,我的目標是讓此方法搜索該文本文件,並用所述「航班號」打印出所有行。無法在原始類型上調用equals(String)int

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

    } 
} 
+2

錯誤是不言自明的 –

回答

1

使用Integer.parseInt將其轉換爲int值,然後使用==

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

通常在飛行數字上沒有算術運算,所以事情應該是字符串數組。

0

fl_number是一個int數組 和 search_flight是一個字符串比較兩個int值,你應該分析它爲int的工作與fl_number

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

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

相關問題