Welcome to Dream.In.Code
Become a Java Expert!

Join 150,385 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,139 people online right now. Registration is fast and FREE... Join Now!




Java Binary Search

 
Reply to this topicStart new topic

Java Binary Search

Captain M
20 Feb, 2008 - 09:15 AM
Post #1

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
I'm trying to program a binary search in java. The program should read a pre-sorted list of words in alphabetical order, and search that list for a word. The problem I'm having is that I need to be able to typecast an object to a string. Here is my code so far:

CODE

import java.util.*;
import java.io.*;

public class BinarySearch
{
  // instance variables
  private int first, middle, last;
  private int[] list;


public static void main (String args[]) {
        // insert code here...
      BufferedReader in = new BufferedReader(new FileReader("words.txt"));
          
           BinarySearch jim = new BinarySearch();
      
       LinkedList Bob = new LinkedList();
      
       String temp;
       while ((temp = in.readLine())!=null){
            Bob.addLast (temp);
       }
      
       System.out.println(jim.search(Bob, "nofx"));
  }

  /**
   * The main search method.
   *
   * @param list - the list to be sorted
   * @param searchTarget - the value to be searched for
   *
   * @return middle or 0 - the location of the requested element is returned
   * if it exists in the list, otherwise 0 is returned
   */
  public int search(LinkedList list, String searchTarget)
  {
    last = list.size() - 1;
    first = 0;
    // while there are still elements to search through
    while (first <= last)
    {
      middle = (first + last) / 2;
      
      // if current middle value is the search target
      if ((list.get(middle)).equals(searchTarget))
      {
        return middle;
      }
      // if current middle value is less than the search target
      else if ((list.get(middle)).compareTo(searchTarget) < 0)
      {
        first = middle + 1;
      }
      // if current middle value is larger than the search target
      else
      {
        last = middle - 1;
      }
    }
    // return 0 if search target not found
    return 0;
  }
}



The error the compiler gives me is:
cannot find symbol : method compareTo(java.lang.String)

and

operator < cannot be applied to java.lang.String,int


Thanks in advance for any help.


User is offlineProfile CardPM
+Quote Post

Captain M
RE: Java Binary Search
20 Feb, 2008 - 09:26 AM
Post #2

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
Sorry guys, I found a solution myself. I guess I'll post it just for reference.

In this line:
CODE
else if ((list.get(middle)).compareTo(searchTarget) < 0)


I just wasn't sure how to tell the program specifically that the object it was comparing to was a string. I did that by switching the statement around to this:

CODE
else if (searchTarget.compareTo((String)list.get(middle)) < 0)



User is offlineProfile CardPM
+Quote Post

capty99
RE: Java Binary Search
20 Feb, 2008 - 09:33 AM
Post #3

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,259



Thanked: 16 times
Dream Kudos: 550
My Contributions
nicely done, always good to post the result, now its searchable and someone will have that problem again.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Java Binary Search
20 Feb, 2008 - 09:40 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Thank you for posting your fix!
User is offlineProfile CardPM
+Quote Post

Captain M
RE: Java Binary Search
20 Feb, 2008 - 10:34 AM
Post #5

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
And one more fix- the less than operator in the above code should be a greater than operator. If this is not fixed, the program outputs 0 everytime.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month