6 Replies Latest reply on Mar 8, 2008 6:43 PM by 807591

    problem with binarysearch function of Arrays

    807591
      i have read a .txt file and loaded its contents to a String array,now i want to search that array for different strings.Im using binarySearch function of Arrays to do this.B4 using binarySearch i do sort the array but the function returns -ve indexes.i displayed the contents of array to check whether the string i was searching for is there or not,that was in the array but the binarySearch doesnt return the right index.i used equalsIgnoreCase with the string to compare and the array[i] having the particular string which is infact equal to the string im comparing to, just to check if there's any problem with the comparison and i found that equalsIgnoreCase wasnt returning true either for the two identical looking strings in console.plz help me
        • 1. Re: problem with binarysearch function of Arrays
          796440
          binarySearch works correctly. The error is in your code. We cannot help you find the error until you post your code.

          When you post code, use the CODE button or [code] and [/code] tags so that it will retain its formatting.
          • 2. Re: problem with binarysearch function of Arrays
            807591
            import java.io.*;
            import java.util.*;
            public class LibrarySystem
            {
                 public static void main(String []args)throws Exception
                 {
                      //loading book file to book array

                      File Books=new File("C:\\books.txt");
                      FileReader in=new FileReader(Books);


                      String InputID=new String();
                      InputID="";
                      int c=0;
                      int code=0;
                      String InputName=new String();
                      InputName="";
                      BufferedReader input= new BufferedReader(in);
                      String line=null;
                      String[] ids=new String[30];
                      while((line=input.readLine())!=null)
                      {
                           StringTokenizer linetoken=new StringTokenizer(line,"*");
                           InputID=InputID+linetoken.nextToken("*");
                      }

                      BufferedReader Input= new BufferedReader(new FileReader(new File("C:\\books.txt")));
                      String lline=null;
                      while((lline=Input.readLine())!=null)
                      {
                           StringTokenizer llinetoken=new StringTokenizer(lline,"*");
                           llinetoken.nextToken("*");
                           InputName=InputName+llinetoken.nextToken("*");
                      }


                      StringTokenizer a=new StringTokenizer(InputID,"-");
                      int BAsize=a.countTokens();
                      String[] array=new String[BAsize];
                      c=0;
                      while(a.hasMoreTokens())
                      {
                           array[c]=a.nextToken("-");

                           c++;
                      }

                      StringTokenizer b=new StringTokenizer(InputName,"-");
                      int Bsize=b.countTokens();
                      String[] array2=new String[Bsize];
                      c=0;
                      while(b.hasMoreTokens())
                      {
                           array2[c]=b.nextToken("-");

                           c++;
                      }
                      System.out.println(Bsize);
                      book[] allBooks=new book[BAsize];
                      c=0;
                      for(c=0;c<BAsize;c++)
                      {
                           allBooks[c]=new book();
                      }
                      c=0;
                      while(c<BAsize)
                      {

                           allBooks[c].setTitle(array2[c]);
                           allBooks[c].setID(array[c]);
                           c++;
                      }


                      //loading student file to student array

                      File Students=new File("C:\\student.txt");
                      FileReader readS=new FileReader(Students);


                      String InputData="";
                      c=0;
                      while((c=readS.read())!=-1)
                      {
                           InputData=InputData+(char)c;
                      }

                      StringTokenizer STokenizer=new StringTokenizer(InputData,"\n");
                      int SAsize=STokenizer.countTokens();
                      String[] Sarray=new String[SAsize];
                      student[] allStudents=new student[SAsize];
                      c=0;
                      while(STokenizer.hasMoreTokens())
                      {
                           Sarray[c]=STokenizer.nextToken("\n");
                           c++;
                      }
                      c=0;
                      for(c=0;c<SAsize;c++)
                      {
                           allStudents[c]=new student();
                      }
                      c=0;
                      System.out.println(SAsize);
                      while(c<SAsize)
                      {

                           //System.out.println(Sarray[c]);
                           allStudents[c].setname(Sarray[c]);
                           //System.out.println(allStudents[c].getName());
                           c++;
                      }

                      //loading teacher file to teacher array

                      File T=new File("C:\\teacher.txt");
                      FileReader readT=new FileReader(T);


                      String InputT="";
                      c=0;
                      while((c=readT.read())!=-1)
                      {
                           InputT=InputT+(char)c;
                      }
                      StringTokenizer tTokenizer=new StringTokenizer(InputT,"\n");
                      int TAsize=tTokenizer.countTokens();
                      String[] Tarray=new String[TAsize];
                      teacher[] allT=new teacher[TAsize];
                      c=0;
                      while(tTokenizer.hasMoreTokens())
                      {
                           Tarray[c]=tTokenizer.nextToken("\n");
                           c++;
                      }
                      c=0;
                      for(c=0;c<TAsize;c++)
                      {
                           allT[c]=new teacher();
                      }
                      c=0;
                      while(c<TAsize)
                      {
                           //System.out.println(Tarray[c]);
                           allT[c].setTname(Tarray[c]);
                           //System.out.println(allT[c].getTname());
                           c++;
                      }

                      //loading issuance file to issuance objects
                      File issuance=new File("C:\\issuance.txt");
                      FileReader readI=new FileReader(issuance);

                      String inputI="";
                      c=0;
                      BufferedReader put= new BufferedReader(readI);
                      line=null;
                      while((line=put.readLine())!=null)
                      {
                           StringTokenizer sh=new StringTokenizer(line,"*");
                           inputI=inputI+sh.nextToken("*");
                      }

                      line=null;
                      String inputIn="";
                      c=0;
                      BufferedReader putt= new BufferedReader(new FileReader(new File("C:\\issuance.txt")));
                      while((line=putt.readLine())!=null)
                      {
                           StringTokenizer sh=new StringTokenizer(line,"*");
                           sh.nextToken("*");
                           inputIn=inputIn+sh.nextToken("*");
                      }

                      line=null;
                      String inputIni="";
                      c=0;
                      BufferedReader puttu= new BufferedReader(new FileReader(new File("C:\\issuance.txt")));
                      while((line=puttu.readLine())!=null)
                      {
                           StringTokenizer sh=new StringTokenizer(line,"*");
                           sh.nextToken("*");
                           sh.nextToken("*");
                           inputIni=inputIni+sh.nextToken("*");
                      }


                      StringTokenizer one=new StringTokenizer(inputI,"-");
                      StringTokenizer two=new StringTokenizer(inputIn,"-");
                      StringTokenizer three=new StringTokenizer(inputIni,"-");
                      c=0;
                      int size=one.countTokens();
                      issuance all[]=new issuance[size];

                      while(c<size)
                      {
                           all[c]=new issuance();
                           all[c].setbID(one.nextToken());
                           all[c].setName(two.nextToken());
                           Boolean bool=new Boolean(three.nextToken());
                           boolean r=bool.booleanValue();

                           all[c].setT(r);
                           //System.out.println(all[c].getName());
                           //System.out.println(all[c].getbID());
                           //System.out.println(all[c].getTcher());

                           c++;
                      }










                      //ttttttttttttttttttttttttttttttttttttttt

                      int choice=0;
                      BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));

                      while(choice != -1)
                      {
                           menu();
                           int lval=0;
                           lval=Integer.parseInt(cin.readLine());

                           if(lval==1)
                           {
                                int bChoice=0;
                                B_Menu();
                                bChoice=Integer.parseInt(cin.readLine());
                                switch(bChoice)
                                {
                                     case 1:
                                     {

                                          int i;
                                          while((i=in.read())!=-1)
                                          {
                                               if(i=='\n')
                                               {
                                                    System.out.println();
                                               }
                                               else
                                               {
                                                    System.out.print((char)i+"");
                                               }
                                          }

                                     }
                                     break;

                                     case 2:
                                     {

                                          System.out.println("Book ID: ");
                                          String one1=cin.readLine();
                                          Arrays.sort(array);
                                          c=0;
                                          if((c=Arrays.binarySearch(array,one1))>=0)
                                          {
                                               System.out.println("book available in Library:");
                                               System.out.println(allBooks[c].getname());
                                          }
                                          else
                                          {
                                               System.out.println("No Such Book Available in Library!");
                                          }

                                     }
                                     break;
                                }
                           }
                           else if(lval==2)
                           {
                                int IChoice=0;
                                I_Menu();
                                IChoice=Integer.parseInt(cin.readLine());
                                switch(IChoice)
                                {
                                     case 1:
                                     {
                                          //****************THIS IS WHERE THE ERROR OCCURS*******************
                                          System.out.println("Student Name: ");
                                          String student=null;
                                          student=cin.readLine();
                                          System.out.println("Book ID: ");
                                          String b2issue=cin.readLine();
                                          Arrays.sort(Sarray);
                                          Arrays.sort(array);

                                          int sIndex=Arrays.binarySearch(Sarray,student);
                                          int bIndex=Arrays.binarySearch(array,b2issue);
                                          //System.out.println(bIndex+"");
                                          c=0;
                                          /*while(c<SAsize)
                                          {
                                               System.out.println(Sarray[c]);
                                               if(Sarray[c].compareTo("anna")==0)
                                               {
                                                    sIndex=c;
                                                    c=SAsize;
                                               }
                                               c++;

                                          }*/
                                          //System.out.println(sIndex+"");
                                          //System.out.println(Sarray[4]);
                                          /*if(Sarray[4].equalsIgnoreCase("sundus "))
                                          {
                                               System.out.println("are equal");
                                          }

                                          //System.out.println(sIndex+"");

                                          if(sIndex<0)
                                          {
                                               System.out.println("Student Not Registered!");
                                          }
                                          else if(bIndex<0)
                                          {
                                               System.out.println("Invalid Book ID!");
                                          }
                                          issuance o=new issuance();
                                          if(o.findStu(all,size,student))
                                          {
                                               System.out.println("Good");
                                          }*/






                                     }
                                     break;
                                }
                           }


                      }
                 }

                 public static void menu()
                 {
                      System.out.println("ENTER INDEX OF MENU TO PROCEED");
                      System.out.println("Books__1");
                      System.out.println("Issuance__2");
                 }
                 public static void B_Menu()
                 {
                      System.out.println("ENTER INDEX OF MENU TO PROCEED");
                      System.out.println("Show All Books__1");
                      System.out.println("Search A Book__2");
                      System.out.println("Find Names of Issued Books__3");
                      System.out.println("Find Number of Issued Books__4");
                 }
                 public static void I_Menu()
                 {
                           System.out.println("ENTER INDEX OF MENU TO PROCEED");
                           System.out.println("Issue Book To Student__1");
                           System.out.println("Issue Book To Teacher__2");
                 }

            }

            sorry its a huge code and iv posted the whole for ur convenience,may it bcome inconvenient for u :)
            • 3. Re: problem with binarysearch function of Arrays
              807591
              i mean mayb it bcoms inconvenient for u
              • 4. Re: problem with binarysearch function of Arrays
                796440
                That's way too much code. You should be able to reproduce your problem in under 30 lines.

                http://mindprod.com/jgloss/sscce.html
                http://homepage1.nifty.com/algafield/sscce.html

                And you didn't bother to use code tags to make it readable.
                • 5. Re: problem with binarysearch function of Arrays
                  807591
                  //loading student file to student array
                            File Students=new File( "C:\\student.txt" );
                            FileReader readS=new FileReader( Students );
                            String InputData="";
                            c=0;
                            while( (c=readS.read( )) != -1)
                            {
                                 InputData = InputData+(char)c;
                            }

                            StringTokenizer STokenizer=new StringTokenizer(InputData,"\n");
                            int SAsize=STokenizer.countTokens();
                            String[] Sarray=new String[SAsize];
                            c=0;
                            while(STokenizer.hasMoreTokens())
                            {
                                 Sarray[c]=STokenizer.nextToken("\n");
                                 c++;
                  }
                  //searchin Student array for students
                            System.out.println("Student Name: ");
                            String student=cin.readLine();
                            Arrays.sort(Sarray);
                            int sIndex=Arrays.binarySearch(Sarray,student);
                            c=0;
                            if(sIndex<0)
                            {
                                 System.out.println("Student Not Registered!");
                            }
                  • 6. Re: problem with binarysearch function of Arrays
                    807591
                    iv solved my problem,the separatos '\n' was causing the problem i dont know how,so i used * as a separator in my file,thank you