473,416 Members | 1,546 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,416 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 8845

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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.