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

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




BinarySearch help

 
Reply to this topicStart new topic

BinarySearch help

wille2511
25 Apr, 2007 - 06:28 PM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 46


My Contributions
It says i have the mid variable is not initailized but it is in the while. I copy this off my text book and I its identical to this code.

CODE
public class BinarySearch
{
    static Scanner blah = new Scanner(System.in);

  public static int binarySearch(int[] list, int listLength, int searchItem)
  {
    int first = 0;
    int last = listLength -1;
    int mid;

    boolean found = false;
    
    while(first <= last && !found)
    {
      mid = (first + last) / 2;
    
      if(list[mid] == searchItem)
        found = true;
      else if(list[mid] > searchItem)
        last = mid - 1;
      else
        first = mid + 1;
    }
      if(found)
       return mid;      
    else
      return -1;

        
  }
}

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: BinarySearch Help
25 Apr, 2007 - 07:23 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
depending on your compiler, it will want an initial value before using it. Even if the first use is an assignment. Try:
int mid = 0;
and see if the error goes away.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:24PM

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