473,287 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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.  
Jun 20 '22 #1
0 8838

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

Similar topics

4
by: dotNetDave | last post by:
I have created my own comparer class using IComparer for use with ArrayList.BinarySearch. My class seems to work with BinarySearch, but the problem is that my ArrayList has three items in it and it...
4
by: Homa | last post by:
I can' believe my own eye, but it happens...there is a bug in ArrayList.BinarySearch!! It should be such a simple function...... Here is the detail. (I'm using C#, don't know if this is C#'s...
2
by: kids_pro | last post by:
Hi there, When class construct it read content from XML file and store it in ArrayList. In my class I had implement Search method using BinarySearch behind. I want to implement another Method...
2
by: Yogi21 | last post by:
Hi I have a sorted array containing strings. I am iterating through the array and clearing the contents one by one using "array.BinarySearch" to find each element. So far so good. But the moment I...
2
by: Mario | last post by:
Hi there, I'm writing a piece of code with VS.Net 2003, Framework 1.1. And I can't make BinarySearch to work right. Look at this sample: Dim sexy() As String = {"-", "a", "b", "ba", "A",...
5
by: Lee | last post by:
I have a custom collection that implements IComarer right now which implements sorting. I've googled and it seems that built in binary search is not available in the underlying List object. If...
11
by: solandre | last post by:
my motivation is as follows: i have to binarysearch several million times an long that is several million items big. this costs time, although i use Array.Binarysearch<long>. so i try to keep...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
2
by: tshad | last post by:
I can do the following with Find: Racer theRacer2 = racers.Find(delegate(Racer racer) { return racer.Car == "Ferrari"; }); But I can't seem to do the same with BinarySearch, this: int iktr...
3
by: endazyar | last post by:
Hey everyone, I'm planning to implement a dictionary on C#. I have about 30,000 words in my database( i keep them in access database right now. Maybe we can make a decision about which database it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.