0

I am working on making an interpolation search to search through a 2d array of bookings for a flight, but due to the values being stored as strings in the 2d array, as there are strings such as the customer name, I can't use the less than or greater than comparators to compare the value of the booking ID which I want to search.

I've tried using the parseInt method to read the value as an int but I still get this error error

code:

 public void searchBookings(String [][]bookings, String bookingToSearch){
        //Static column will be searched (bookingID)
        int column = 0;
        int highEnd = (bookings.length-1);
        int lowEnd = 0;
    
        while (bookingToSearch >= Integer.parseInt(bookings[lowEnd][column])){}
  • 1
    your bookingToSearch is also a String, so you would have to cast this too. – Hias May 08 '22 at 16:26
  • Possible duplicate: https://stackoverflow.com/questions/5153496/how-can-i-compare-two-strings-in-java-and-define-which-of-them-is-smaller-than-t – markspace May 08 '22 at 16:31
  • Summary: You can use `str1.compareTo(str2);` to compare strings for >= and <=. See that link for details. – markspace May 08 '22 at 16:32
  • As a general rule, you should never use arrays; only use collections (ie Lists). Secondly, field/parameter types should be correct for what they represent. If you fix that, your problem should vanish. Also, name classes with a leading uppercase letter. – Bohemian May 08 '22 at 16:38
  • 1
    Why would you *not* be checking for id equality if you're searching for a booking? – g00se May 08 '22 at 16:42
  • Thank you for pointing that out hias, I forgot I converted it to string to begin with since intelliJ didn't like it being an int. – BaileyLylyk May 08 '22 at 17:01

0 Answers0