470,831 Members | 1,373 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,831 developers. It's quick & easy.

BinarySearch Dictionary

Hello everyone, I tried in this code to make a binarysearch dictionary in a text file using ArrayList and socket, but I don't know what happen that I can't do this..?
Client code:
Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7. import java.util.Arrays;
  8.  
  9. public class Client {
  10.  
  11.     public static void main(String[] args) {
  12.         int N;
  13.         Scanner sc=new Scanner(System.in);
  14.         Socket cs=null;
  15.         BufferedReader bfr=null;
  16.         PrintWriter pw=null;
  17.         try{
  18.             cs=new Socket("localhost",5000)     ;
  19.             InputStreamReader isr=
  20.                     new InputStreamReader(cs.getInputStream());
  21.             bfr=new BufferedReader(isr);
  22.             pw=new PrintWriter(cs.getOutputStream());
  23.             System.out.println("S-a setat reteaua");
  24.             for(;;){
  25.  
  26.                 System.out.print("Se introduce cuvantul: ");
  27.                 String cuvant=sc.nextLine();
  28.                 //daca este STOP : se va deconecta
  29.                 if(cuvant.equals("STOP")) {pw.println(""); pw.flush();break;}
  30.                 else  {pw.println(cuvant); pw.flush();}
  31.                 //Citim raspuns server:
  32.                 String textIn=bfr.readLine();
  33.                 if(textIn==null)break;
  34.                 System.out.println(textIn);
  35.             }//for;;
  36.         }catch( IOException e){
  37.             e.printStackTrace();
  38.         }
  39.         System.out.println("Client m-am deconectat !");
  40.     }//main
  41. }
  42.  
ClientHandler:
Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7. import java.util.Arrays;
  8.  
  9. public class Client {
  10.  
  11.     public static void main(String[] args) {
  12.         int N;
  13.         Scanner sc=new Scanner(System.in);
  14.         Socket cs=null;
  15.         BufferedReader bfr=null;
  16.         PrintWriter pw=null;
  17.         try{
  18.             cs=new Socket("localhost",5000)     ;
  19.             InputStreamReader isr=
  20.                     new InputStreamReader(cs.getInputStream());
  21.             bfr=new BufferedReader(isr);
  22.             pw=new PrintWriter(cs.getOutputStream());
  23.             System.out.println("S-a setat reteaua");
  24.             for(;;){
  25.  
  26.                 System.out.print("Se introduce cuvantul: ");
  27.                 String cuvant=sc.nextLine();
  28.                 //daca este STOP : se va deconecta
  29.                 if(cuvant.equals("STOP")) {pw.println(""); pw.flush();break;}
  30.                 else  {pw.println(cuvant); pw.flush();}
  31.                 //Citim raspuns server:
  32.                 String textIn=bfr.readLine();
  33.                 if(textIn==null)break;
  34.                 System.out.println(textIn);
  35.             }//for;;
  36.         }catch( IOException e){
  37.             e.printStackTrace();
  38.         }
  39.         System.out.println("Client m-am deconectat !");
  40.     }//main
  41. }
  42.  
Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8.  
  9. import static java.util.Collections.binarySearch;
  10.  
  11. //Clasa firului de execuție pentru tratarea clientului conectat la server:
  12. class FirClient extends Thread {
  13.     private Socket cs;
  14.     private BufferedReader bfr;
  15.     private PrintWriter pw;
  16.     ArrayList<String> listaucvinte;//la creare firului incarcam datele
  17.     // din fisier intr-un ArrayList
  18.  
  19.  
  20.     public FirClient(Socket cs) {
  21.         try {
  22.             this.cs = cs;
  23.             pw = new PrintWriter(cs.getOutputStream());
  24.             InputStreamReader isr =
  25.                     new InputStreamReader(cs.getInputStream());
  26.             bfr = new BufferedReader(isr);
  27.             //incarcam fisierul cuvinte .txt in ArrayList:
  28.             listaucvinte = new ArrayList<String>();
  29.             System.out.println("Se copiaza fisier in al");
  30.             FileReader f = new FileReader("D://date1.txt");
  31.             BufferedReader bf = new BufferedReader(f);
  32.             for (; ; ) {
  33.                 String s = bf.readLine();
  34.                 if (s == null) break;//s-a terminat fisier
  35.                 listaucvinte.add(s);
  36.             }
  37.             listaucvinte.sort(String::compareTo);
  38.             bf.close();
  39.             f.close();
  40.         } catch (Exception e) {
  41.             e.printStackTrace();
  42.             System.exit(1);
  43.         }
  44.     }
  45.  
  46.     public static int CautareBinara(ArrayList<String> listaucvinte, String cuvant) {
  47.         int start = 0;
  48.         int sfarsit = (listaucvinte.size()) - 1;
  49.  
  50.  
  51.         while (start <= sfarsit) {
  52.             int mijloc = start + (sfarsit - start) / 2;
  53.             if (listaucvinte.get(mijloc).compareTo(cuvant) < 0) {
  54.                 start = mijloc + 1;
  55.             } else if (listaucvinte.get(mijloc).compareTo(cuvant) > 0) {
  56.                 sfarsit = mijloc - 1;
  57.             } else {
  58.                 return mijloc;
  59.             }
  60.         }
  61.         return -1;
  62.     }
  63.  
  64.     public void run() {
  65.         try {
  66.             for (;;) {
  67.                 String cuvant = bfr.readLine();
  68.                 if (cuvant == null) break;
  69.                 if (cuvant.equals("")) break;
  70.                 //cautare cuvant in ArrayList:
  71.                 for (int i = 0; i < listaucvinte.size(); i += 2) {
  72.                     String crt = listaucvinte.get(i);
  73.                     int r = CautareBinara(listaucvinte, cuvant);
  74.                     if (r == -1)
  75.                         pw.println("Elementul nu s-a gasit!");
  76.                                else
  77.                     pw.println("Elementul este prezent la indexul " + cuvant);
  78.                     pw.flush();
  79.                 }
  80.  
  81.  
  82.             }//for;;
  83.         }
  84.         catch(Exception e){e.printStackTrace();}
  85.     }
  86. }
  87.  
1 Week Ago #1
0 3219

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by dotNetDave | last post: by
4 posts views Thread by Homa | last post: by
2 posts views Thread by kids_pro | last post: by
2 posts views Thread by Yogi21 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.