Ask your own question, for FREE!
Computer Science 24 Online
OpenStudy (anonymous):

Getting errors with my Java Program. I have to do a simple program that uses binary search to pick a value out of an array. Here's what I have-

OpenStudy (anonymous):

public class Bsearch { public static final int NOT_FOUND= -1; public static int binarySearch( int [] a, int x) { int low= 0; int high = a. length - 1; int mid; while( low <= high) { mid = ( low + high ) / 2; if( a[ mid ].compareTo( x ) < 0 ) low = mid + 1; else if( a[ mid ].compareTo( x ) > 0 ) high = mid - 1; else return mid; } return NOT_FOUND; } public static void main(String[] args) { int SIZE = 6; int [] a = new Integer [ SIZE ]={ 10,-3,5,24,45.3,10.5 }; System.out.println("45.3 Found" +binarySearch(a, 45.3)); } }

OpenStudy (anonymous):

All my errors are coming from this section int SIZE = 6; int [] a = new Integer [ SIZE ]={ 10,-3,5,24,45.3,10.5 }; System.out.println("45.3 Found" +binarySearch(a, 45.3));

OpenStudy (anonymous):

it would help if you gave us the stack trace

OpenStudy (anonymous):

Hi. Its because you use int and Integer simultaneoulsy for the declaration. It is not same. "int" is a primitve data type. "Integer" is an object. http://mindprod.com/jgloss/intvsinteger.html refer this link for the difference. Also, you cannot use two assignment operations combined. try using this line instead : int[] a = {10,-3,5,24,45.3,10.5}; It automatically assumes that the size is 6. If you want the size to be as you desire, you can use this: int[] a=new int[SIZE]; a= {10,-3,5,24,45.3,10.5}; In this case the size of the array will be the value of "SIZE". Smalll Tip : Make sure the value of "SIZE" is more than the number you are storing in it.

OpenStudy (anonymous):

Also, I noticed that the binarySearch() you have defined, will only work for already sorted array. So, kindly sort the array before you pass the array to the function. Tip : There are lots of sorting functions you can read about. Bubble sort, Selection sort are quite easy to understand.

OpenStudy (woodrow73):

``` int [] a = new Integer [ SIZE ]={ 10,-3,5,24,45.3,10.5 }; ``` 45.3 is not an int, it's a float or double; that will contribute towards an error in your program.

OpenStudy (woodrow73):

'Narrowing conversions' are not automatic in java; just Widening Conversions; this is to prevent data loss.. A narrowing conversion is the move to a smaller data-type(in terms of memory), and a widening conversion is the move to a bigger data-type. so if you manually did a narrowing conversion on 45.3 to an int, you would need to use a cast operator. ``` int a = (int)45.3; ``` but alas, ints do not store decimals, so the value stored in 'a' is now 45.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!