Results 1 to 6 of 6
  1. #1
    MylesPollie is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Writing a binary search program and keep getting errors

    Java Code:
    public class Search
    {
    	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 ] ={-3,10,5,24,45.3,10.5};
    			System.out.println("43.5 found at " +binarySearch(a,43.5));
    	}
    }

    I keep getting illegal start of expression at ={-3,10,5,24,45.3,10.5}
    It says I have 12 errors and I have tried so many things to fix it and I just don't know what to change to make it work. Thanks.
    Last edited by JosAH; 02-24-2014 at 06:37 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Writing a binary search program and keep getting errors

    45.3 and 10.5 are not integer numbers, nor is 43.5 (and the array is not sorted and an Integer is not an int) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Writing a binary search program and keep getting errors

    Indeed, there are quite a few things that are wrong with it as Jos mentioned...

    The "illegal start of expression" error message in particular refers to the line int [ ] a = new Integer [ SIZE ] ={-3,10,5,24,45.3,10.5};. 'a' is a primitive type integer array (indicated by int[]), but you're trying to assign to it an array of Integer objects (indicated by new Integer [ SIZE ]). What type of array do you really want 'a' to be, considering:
    • the binarySearch method accepts a primitive integer array as a parameter, but
    • you're also trying to execute the compareTo method against the primitive type integer array a[]?


    (Primitive data types such as integer does not have methods such as compareTo. However Integer objects do.)

    I suppose you may be unsure of the difference between a primitive type integer and an Integer object. Take a look at the class description for Integer at Integer (Java Platform SE 7 ) and see if this makes sense to you.

    On top of that, as Jos mentioned, you're trying to initialise the array with non-integer numbers.

    The syntax that follows [ SIZE ] is also not the right way to initialise the array. See Javanotes 6.0, Section 7.1 -- Creating and Using Arrays on how to initialise arrays.

    Do go through the links, and then try to work it out. If after that your code still fails to compile, post your modified code here.

  4. #4
    MylesPollie is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Writing a binary search program and keep getting errors

    Okay, I changed it to this

    public class Search
    {
    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 )
    {
    list = new int[] {3,10,5,24,45,10};
    System.out.println("43.5 found at " +binarySearch(a,45));
    }
    }


    but I'm still getting 4 errors. What I did probably doesn't make much sense. Sorry, I'm really inexperienced with this.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Writing a binary search program and keep getting errors

    Well,


    1. You can't invoke compareTo on a primiitve (it requires an object that has implemented Comparable<T>). So just use <, > , etc.
    2. list is not a declared type.
    3. You can't do a binary search on an unsorted list (already mentioned in earlier post).
    4. And you are passing 'a' to your binarySearch. Where is 'a' coming from?


    Regards,
    Jim
    Last edited by jim829; 02-24-2014 at 08:40 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Writing a binary search program and keep getting errors

    Declaring and initializing arrays:

    Java Code:
    int[] temp = new int[10];
    //OR
    int[] temp = {5,2,6,4,1,0};
    classname name = new classname(parameters); is not used for primitive data types!

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2013, 09:40 AM
  2. Replies: 1
    Last Post: 12-01-2012, 03:20 AM
  3. Need help debugging Binary search program
    By cotemaria in forum New To Java
    Replies: 6
    Last Post: 11-13-2011, 09:37 PM
  4. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 01:42 AM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •