Link to home
Start Free TrialLog in
Avatar of Arkajyoti Pal
Arkajyoti Pal

asked on

How can I use Binary Search on a sorted array of string in Java?

I know how to use Binary Search for numeric values. However, I need to use Binary Search for searching the name from an already sorted array of string.
See line 63
//Updation of mobile nos. for n customers
import java.util.Scanner;
class MobileNo
{
     private long mobNo[];
     private int n;
     private String name[];
     void getInput()
     {
         Scanner sc = new Scanner(System.in);
         do
         {
             System.out.print("Enter the no. of customers: ");
             n = sc.nextInt();
         }
         while(n <= 0);
         System.out.println("Enter the customer names and mobile nos: ");
         name = new String[n];
         mobNo = new long[n];
         for(int i = 0; i < n; i++)
         {
             System.out.println("Customer - "+(i + 1)+":- ");
             if(sc.hasNextLine())
             sc.nextLine();//drains Scanner of Line Feeds
             name[i] = sc.nextLine();
             do
             {
                 mobNo[i] = sc.nextLong();
                 if(mobNo[i] < 1000000000l || mobNo[i] > 9999999999l)
                 System.out.println("Give a 10 digit no!");
             }
             while(mobNo[i] < 1000000000l || mobNo[i] > 9999999999l);
         }
     }
     void sort()
     {
         String temp;
         long tempNo;
         for (int i = 0; i < n; i++) 
         {
               for (int j = i + 1; j < n; j++) 
               {
                   if (name[i].compareTo(name[j])>0) 
                   {
                       temp = name[i];
                       tempNo = mobNo[i];
                       name[i] = name[j];
                       mobNo[i] = mobNo[j];
                       name[j] = temp;
                       mobNo[j] = tempNo;
                   }
               }
         }
         System.out.println("Names in Alphabetical Order:");
         for (int i = 0; i < n - 1; i++) 
         {
              System.out.println(name[i] + " : " + mobNo[i]);
         }
         System.out.println(name[n - 1] + " : " + mobNo[n - 1]);
     }
     void update()
     {
         //this is where I need to use a Binary Search algorithm for a sorted array of string
     }
     void display()
     {
         for(int i = 0; i < n; i++)
         {
             System.out.println(name[i] + " : " + mobNo[i]);
         }
     }
}

Open in new window

//Updation of mobile nos. for n customers(main method)
class MobileNo_main
{
    public static void main(String args[])
    {
        MobileNo update = new MobileNo();
        update.getInput();
        update.display();
        update.sort();
        update.update();
    }
}

Open in new window

SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And because you've used parallel arrays, you won't be able to maintain the connection between the name and number when you search the array
Avatar of Arkajyoti Pal
Arkajyoti Pal

ASKER

Problem resolved!
PS: @CEHJ I noted that you advised me on not using parallel arrays as I won't be able to maintain the connection. However, I still found a way to maintain the connection till the end using parallel arrays. ;)