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
Question
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]);
}
}
}
Solution
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
OTHER TIPS
There is usually no arithmetic to perform on flight numbers, so things should be String arrays.
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)