Results 1 to 4 of 4
  1. #1
    billy is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Binary search problem

    problem solved
    Last edited by billy; 10-11-2010 at 02:48 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    Saying "it doesn't work" is pretty meaningless. How doesn't it work? What does it do differently from what you expect? Why do you think that is? Do you get any exceptions? What have you tried? What worked, and what didn't work? Why didn't it work? Be specific, and post a short piece of runnable code that demonstrates the problem.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Java Code:
     		System.out.print( "Enter the item to be found " );
      		line = thing.readLine(); // line is a string
    		search_item = Integer.valueOf(line).intValue(); // convert to int
    first:
    why do you want to convert it to an integer? ;/
    second:
    if (search_item < a[middle]) makes no sense, because the type of a[middle] is contacts, search_item is an integer u cant compare them like you do.
    you have to compare the strings! e.g. line.compareTo(a[middle].surname)

    not tested and based on your code:
    Java Code:
    		BufferedReader thing;
    		thing = new BufferedReader(new InputStreamReader(System.in));
    		int upper, lower, middle;
    		System.out.print("Enter the item to be found ");
    		String line = thing.readLine(); // line is a string
    		lower = 0;
    		upper = a.length - 1;
    		do {
    			middle = (lower + upper) / 2;
    			if (line.compareTo(a[middle].surname) < 0)
    				upper = middle - 1;
    			else
    				lower = middle + 1;
    		} while ((!a[middle].surname.equals(line)) && (lower <= upper));
    
    		if (a[middle].surname.equals(line))
    			System.out.print("The item is at position " + middle);
    		else
    			System.out.print("This item is not found");

  4. #4
    billy is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Custom binary search problem
    By PtblDuffy in forum New To Java
    Replies: 2
    Last Post: 09-16-2010, 09:53 PM
  2. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 02:42 AM
  3. Binary Search Problem
    By sharpnova in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 11:22 AM
  4. Help. Binary Search Problem
    By Krooger in forum Advanced Java
    Replies: 1
    Last Post: 11-03-2008, 07:19 AM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 04: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
  •