2
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by NormR1
0

Okay here is the code

public void Search()
   {
      System.out.println("Who do you want to find? ");
      System.out.println("Last Name: ");

      String Value;
      doubleNode Temp;
      Temp = list.getBegin();

      int k = 0;
      String [] lis = {"","","","","","","","","","","","","","","","","","","",""};
      String [] result = {"","","","","","","","","","","","","","","","","","","",""};


      while( (Value = Temp.getValue()) != null)
      {
         lis [k] = Value;
         String [] tokens = lis [k].split(" ");
         result [k] = tokens[3];
         Temp = Temp.getNext();
         k++;
         if(Temp == null)
            break;
      }

      String Name = Input.next();
      int index = Arrays.binarySearch(result, Name);

      if (index >= 0 )  
         System.out.println(Name + " is in the Roster");  
      else  
         System.out.println(Name + " is not in the Roster.");  

   }
0

It seems that the problem has something to do with the binary search, but I don't know what it is. The idex result is always -21.

0

Can you post the output from the program. Have you read the API doc for the binareySearch method to see what values it returns? Why do you think -21 is wrong?
When does the array; result get any non empty values? Have you printed out the contents of result to see what it is loaded with? How many elements does it have? Change the number of elements and see if the value of index changes.

Edited by NormR1

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.