DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005

    How to implement binary search

    I have a text file containing several words and pointers, like this:

    String1 int1
    String2 int2
    String3 int3

    The String1, String2, String3......are sorted in lexicographical order.

    What I want to do is to search a word among those Strings, using binary search, and return the int value beside the matched String.

    Could anybody give me some detailed help on this question? Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    do you know the details of the binary search algorithm? do you need help with that or do you need help implementing it in java?

    Kind regards,
    Efficiency is intelligent laziness

  3. #3
    Join Date
    Dec 2004
    San Bernardino County, California
    First you have to implement a method which processes the content of the file.
    The Scanner class will help immensely in that process - I would ask my implementation to tokenize the text using the new line symbol as the delimiter. The tokens will be put into a collection.

    You can implement your own "search" of the String, or you can use the Scanner class's findInLine() method. If your current String contains the pattern, then use the nextInt() method to obtain the integer accompanying the String; then do next() to get the next token, and repeat.

    There are some other methods of the Scanner class which can search across the entire file, ignoring delimiters.

Similar Threads

  1. Binary Search
    By Dreamer in forum Java
    Replies: 0
    Last Post: 01-30-2006, 02:56 PM
  2. Replies: 1
    Last Post: 12-17-2005, 02:50 PM
  3. Binary Search Tree implementation
    By Liza in forum Java
    Replies: 0
    Last Post: 10-23-2001, 11:21 AM
  4. Binary Search trees
    By Tom in forum VB Classic
    Replies: 0
    Last Post: 12-02-2000, 12:24 PM
  5. Binary Search trees
    By Tom in forum Java
    Replies: 0
    Last Post: 12-02-2000, 12: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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.