Home > Net >  The first element of the string array isn't being found
The first element of the string array isn't being found

Time:07-10

I am trying to match queries with elements in the string array but when I try to search for the first element entered into the array before sorting, the result shows that the element was not found. What is wrong with the code?

import java.util.*;
import java.io.*;

class Solution{
    public static void main(String []argh)
    {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        in.nextLine();
        String name[]= new String[n];
        int phone[]= new int[n];
        for(int i=0;i<n;i  )
        {
            name[i]=in.nextLine().trim();
            phone[i]=in.nextInt();
            in.nextLine();
        }
        
        for(int i=0;i<n-1;i  )
        {
            for(int j=0;j<n-i-1;j  )
            {
                if(name[j].compareTo(name[j 1])>0)
                {
                   String temp=name[j];
                   name[j]=name[j 1];
                   name[j 1]=temp; 
                   int temp2=phone[j];
                   phone[j]=phone[j 1];
                   phone[j 1]=temp2;
                }
            }
        }//bubble sort
        
        
      
        
        
        while(in.hasNext())
        {
            String s=in.nextLine().trim();           
            //System.out.println(s);
            boolean b=true;
            int lp=0,up=n,mid=0;
            
            while(lp<up)
            {   mid=(lp up)/2;
            
                if(s.compareTo(name[mid])==0)
                {
                    System.out.println(s "=" phone[mid]);
                    b=false;
                    break;
                    
                    
                }
                else if(s.compareTo(name[mid])<0)
                {
                    up=mid-1;
                    
                }
                else
                {
                    lp=mid 1;
                }
            }

            if(b)
            {
                System.out.println("Not found");
            }
            
        
        }//binary search


    }//main ends
}//class ends

The above code was written as a sort of telephone directory which accepts names and a corresponding 8 digit phone number. It first accepts the number of elements or entries in the directory followed by the elements(the names and corresponding phone number). The data is then sorted using the bubble sort algorithm.

The program then takes string queries which are names until the end of file is reached. If a query is found to be an exact match with an element in the String array then it prints the string along with the phone number.If there is no match it prints "Not found".

Eg:

Input:

 4
 Anon1
 12345678
 Anon2
 12348765
 Anon3
 87654321 
 Anon4 
 87651234
 Anon3
 Anon1
 Anon5

Expected Output:

Anon3=87654321
Anon1=12345678
Not found

However the result I am getting differs from the expected result as follows. Output:

Anon3=87654321
Not found
Not found

The first element I enter (in this case Anon1) does not turn up any matches. What is wrong with the code?

CodePudding user response:

Your while loop causes this problem. The first element has the index 0 and as long as lp < up 0 is not reachable. Change the smaller than to a smaler equals and you're good.

In addition I would add a check that up doesn't get smaller than 0 because you are decreasing it. You while loop could look like this:

while (lp <= up && up >= 0) {
  • Related