Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem with a binary search

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Problem with a binary search

    I am attempting to write basic code for a binary search. Here is the code I have:

     
    public class BinarySearch
    {
        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, 5, 10, 10.5, 24, 45.3};
            System.out.println("45.3 found at " + binarysearch(a,45.3));
        }
    }

    However, when I attempt to compile, I am given a message that says this line of code:
    int [] a = new Integer[SIZE] = {-3, 5, 10, 10.5, 24, 45.3};
    has an "illegal start of expression". I don't know what this means much less how to fix it.

    Help would be much appreciated. Thank You!


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with a binary search

    Quote Originally Posted by AWol View Post
    However, when I attempt to compile, I am given a message that says this line of code:
    int [] a = new Integer[SIZE] = {-3, 5, 10, 10.5, 24, 45.3};
    If the variable is int[], you must instantiate an int[] .... not an Integer[]. And the syntax is also wrong for 2 reasons:
    a) The '=' before '{' is wrong.
    b) If you specify the size in [ ] you can't put { } with values. Either a size, or initialized values ... never both.

    Thus:
    int[] a = new int[] {-3, 5, 10, 10.5, 24, 45.3};
    or more simply
    int[] a = {-3, 5, 10, 10.5, 24, 45.3};
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    AWol (January 7th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a binary search

    Thank You!

    Another error is popping up now, saying that this line:

    if(a[mid].compareTo(x) < 0)

    is not good. The error says "int cannot be dereferenced".

    I am taking a beginning class which is very very poorly put together, and all of your help is really really appreciated. Thank You so much!!

  5. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with a binary search

    Quote Originally Posted by AWol View Post
    if(a[mid].compareTo(x) < 0)

    is not good. The error says "int cannot be dereferenced".
    An int is not an object .....
    Compare ints simply using relational operators (< , > , etc ...)
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  6. The Following User Says Thank You to andbin For This Useful Post:

    AWol (January 7th, 2014)

  7. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a binary search

    I'm sorry I'm just not following... Like I said this class will explain what you're supposed to do, give a short example (in which I'm finding out the code is not even valid) and say "ok now do it"... So I'm sort of lost.

    What would I have to change it to?

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,495
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Problem with a binary search

    A method, like compareTo(), cannot be called on a primitive. Imagine a[mid] = 5, then 5.compareTo( anything ) is not valid. You would simply use comparison operators, 5 < anything, 5 > anything, 5 == anything, etc.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    AWol (January 7th, 2014)

Similar Threads

  1. Problem with Binary Search Tree
    By hawkflame in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 25th, 2012, 09:04 PM
  2. Binary Search Help
    By OwsumEmam in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 31st, 2011, 11:01 PM
  3. Binary Search Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 15th, 2011, 04:03 PM
  4. Binary Search Loop
    By Tarakara in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 29th, 2010, 08:22 AM
  5. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 08:27 AM