Reputation: 5
I was doing the binary search for String and it showing output error. I do not know what I missing and I need some advice.
Here my code :
public static final int Not_Found = -1;
public static int BS( String[][] record, String x )
{
int low = 0;
int high = record.length - 1;
int mid;
while( low <= high )
{
mid = ( low + high ) / 2;
if( record[ mid ].compareTo( x ) < 0 )
low = mid + 1;
else if( record[ mid ].compareTo( x ) > 0 )
high = mid - 1;
else
return mid;
}
return Not_Found;
}
Its that I missing something? or I have to use other way to find it?
Here the error :
error: cannot find symbol if( record[ mid ].compareTo( x ) < 0 ) ^ symbol: method compareTo(String)
error: cannot find symbol else if( record[ mid ].compareTo( x ) > 0 ) ^ symbol: method compareTo(String)
Upvotes: 0
Views: 237
Reputation: 3858
you are using "String[][]" means 2D record. So, record[j] gives corresponding jth column string array. Which can't be compared to a string. So, use "String[] record" if it is 1d record.
Upvotes: 1
Reputation: 129507
Well record
is a 2D-array, so record[j]
will give an array as opposed to a string. Did you maybe mean to use a 1D-array instead?
Upvotes: 1