Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The next column word as translate

Status
Not open for further replies.

mihailmihai988

Programmer
May 6, 2022
1
RO
Hello everyone, in this code I can search any word that I want in a text file using Socket , but the problem is... I need to read also the next column after I read the the word that I need to translate. I found I can do this with method readLine , but I'm not sure about this and I don't know how...
Server:
Java:
import java.net.ServerSocket;
import java.net.Socket;
 
public class Server {
 
    public static void main(String[] args) {
        System.out.println("Am pornit server...");
           try{
            ServerSocket ss=new ServerSocket(5000);
            for(;;){
             Socket cs=ss.accept();
             FirClient firPtClient=new FirClient(cs);
             firPtClient.start();
             System.out.println("Avem o conectare!");
            }//for;;
           }catch(Exception e){
               e.printStackTrace();
           }
 
 
    }
 
}

Java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
 
//Clasa firului de execuție pentru tratarea clientului conectat la server:
class FirClient extends Thread
{
  private Socket cs;
  private BufferedReader bfr;
  private PrintWriter pw;
  ArrayList<String> al;//la creare firului incarcam datele
                                      // din fisier intr-un ArrayList
  public FirClient(Socket cs)
  {
   try{
      this.cs = cs;
      pw = new PrintWriter(cs.getOutputStream());
      InputStreamReader isr = 
                 new InputStreamReader(cs.getInputStream());
      bfr = new BufferedReader(isr);
      //incarcam fisierul cuvinte .txt in ArrayList:
      al=new ArrayList<String>();
      System.out.println("Se copiaza fisier in al");
      FileReader f=new FileReader("D:\\date.txt\\");
      BufferedReader bf=new BufferedReader(f);
      for(;;){
          String s=bf.readLine();
          if(s==null)break;//s-a terminat fisierul
          al.add(s);
      }
      bf.close();
      f.close();
    }catch(Exception e){
         e.printStackTrace();
         System.exit(1);}
  }
 
  public void run()
  {
   try{
       for(;;){
        String cuvant = bfr.readLine();
        if(cuvant == null)break;
        if(cuvant.equals(""))break;
        //cautare cuvant in ArrayList:
        boolean gasit=false;
        for(int i=0;i<al.size();i++){
            String crt=al.get(i);
            if(crt.equals(cuvant)){
               gasit=true;
               break;
            }
        }
        if(gasit)pw.println("da, este prezent si inseamna: ");
        else pw.println("nu, nu este prezent");
        pw.flush();
      }//for;;
     }catch(Exception e){e.printStackTrace();}
  }//run
}

Client:
Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
 
public class Client {
 
    public static void main(String[] args) {
        int N;
           Scanner sc=new Scanner(System.in);
           Socket cs=null;
           BufferedReader bfr=null;
           PrintWriter pw=null;
           try{
            cs=new Socket("localhost",5000)    ;
            InputStreamReader isr=
              new InputStreamReader(cs.getInputStream());
            bfr=new BufferedReader(isr);
            pw=new PrintWriter(cs.getOutputStream());
            System.out.println("S-a setat reteaua");
            for(;;){
              
               System.out.print("Cuvantul care trebuie tradus:");
               String cuvant=sc.nextLine();
               //daca este STOP : se va deconecta
               if(cuvant.equals("STOP")) {pw.println(""); pw.flush();break;}
               else  {pw.println(cuvant); pw.flush();}
               //Citim raspuns server:
               String textIn=bfr.readLine();
               if(textIn==null)break;
               System.out.println(textIn);
            }//for;;
           }catch( IOException e){
               e.printStackTrace();
           }
           System.out.println("Client m-am deconectat !");
    }//main
}
I put also the text file to help more if I didn't explain corectly.
 
 https://files.engineering.com/getfile.aspx?folder=092f0a15-759b-4a98-9685-7784680335ca&file=date.txt
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top