Results 1 to 3 of 3
  1. #1
    PtblDuffy is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default Custom binary search problem

    Hey all, been having a bit of trouble with this piece of my code for a binary search. It's part of a bigger project, but basically what it needs to do is look for a "Creature" by the creature's name.
    Java Code:
    import java.util.ArrayList;
    
    public class BinarySearch {
    
        public static Creature binarySearch(ArrayList<Creature> list, String creatureName) {
            Creature retVal = null;
            int size = list.size();
            if (size > 0) {
                System.out.println(list);
                int index = list.size() / 2;
                Creature mid = (Creature) list.get(index);
                String midName = mid.name;
                System.out.print("midName: " + midName + " || mid: " + mid);
                System.out.println(" || creatureName: " + creatureName);
                ArrayList<Creature> temp = new ArrayList();
    
                if (midName.compareTo(creatureName) < 0) {
                    for (int i = index; i < size; i++) {
                        temp.add((Creature) list.get(i));
                    }
                    binarySearch(temp, creatureName);
                } else if (midName.compareTo(creatureName) > 0) {
                    for (int i = 0; i < index; i++) {
                        temp.add(list.get(i));
                    }
                    binarySearch(temp, creatureName);
                } else if (midName.equalsIgnoreCase(creatureName)) {
                    System.out.println("Match Found");
                    retVal = mid;
                    System.out.println("retVal = " + retVal);
                }
            }
            System.out.println("retVal = " + retVal);
            return retVal;
    
        }
    }
    For a reason I can't figure out, when it reaches the "return retVal;" line, it runs through the binarySearch method again. Can anyone tell as to why this is happening? No matter what I do, the method always returns null.

    Here is some example output:

    Java Code:
    [Ema, NPC, a student who always writes Java projects on time, Jeremy, NPC, a student who writes Java projects in the very last moment, Lewell, NPC, a student who doesn't even know what a Java project is, Lily, Animal, a black and white cat, Lucy, Animal, a pink pig, Martina, NPC, a student who doesn't write Java projects at all, Mary, NPC, a tall woman, Mutsu, Animal, a black-spotted pig, Peter, Animal, a brown dog, Squash, PC, one who doesn't leave the animals and NPC-s alone]
    midName: Martina || mid: Martina, NPC, a student who doesn't write Java projects at all || creatureName: Lily
    [Ema, NPC, a student who always writes Java projects on time, Jeremy, NPC, a student who writes Java projects in the very last moment, Lewell, NPC, a student who doesn't even know what a Java project is, Lily, Animal, a black and white cat, Lucy, Animal, a pink pig]
    midName: Lewell || mid: Lewell, NPC, a student who doesn't even know what a Java project is || creatureName: Lily
    [Lewell, NPC, a student who doesn't even know what a Java project is, Lily, Animal, a black and white cat, Lucy, Animal, a pink pig]
    midName: Lily || mid: Lily, Animal, a black and white cat || creatureName: Lily
    Match Found
    retVal = Lily, Animal, a black and white cat
    retVal = Lily, Animal, a black and white cat
    retVal = null
    retVal = null

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

    Default

    You binarySearch( ... ) method seems to return a Creature when it found it; you're also calling this method recursively but you ignore its return value; so make those recursive invocations:

    Java Code:
    return binarySearch(temp, creatureName);
    ... otherwise those returns of the calls will continue in your current invocation of that method.

    kind regards,

    Jos

  3. #3
    PtblDuffy is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 01:42 AM
  2. Binary Search Problem
    By sharpnova in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 10:22 AM
  3. Help. Binary Search Problem
    By Krooger in forum Advanced Java
    Replies: 1
    Last Post: 11-03-2008, 06:19 AM
  4. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 07:13 PM
  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
  •