0

I have following code. One reads a File called "denglish.txt" splits it into words and sends every 2nd word over Tcp socket connection to a server. The Server is now supposed to read this word and compare a to a word from a dictionary. Weird thing is the comparison always fails if i send it that way. But if the clients sends something like out.Print("make") it works . I really dont understand why its like this. I can send and print out the word from the client on the Server and he prints out the correct words but the stringcompare fails. MAybe someone can help me. THank you guys

    Socket socket = new Socket ("localhost" , 47777);
        PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
        //Socket socket1 = new Socket ("localhost" , 47778);
        //PrintWriter out1 = new PrintWriter(socket1.getOutputStream(),true);


            String line;
            BufferedReader text = new BufferedReader(new InputStreamReader(new FileInputStream("denglisch.txt")));
            String text1 = "";


           while((line = text.readLine()) != null) {  // handle lines
                text1 = text1 + line;
                }

           int i = 0;
           //out.println("make");
           StringTokenizer tokenizer = new StringTokenizer( text1,",.\" : !" );
           while ( tokenizer.hasMoreTokens() )
           {
                if (i % 2 == 0)
                    {

                    out.println(tokenizer.nextToken().toLowerCase().toString());
                    out.flush();
                    }
                if (i % 2 ==1 )
                    {
                    //out1.println(tokenizer.nextToken());
                    }
                i = i+1;


Server CODE
    //Server Codepublic




<pre>   filteritserver(BufferedReader dict, String plusminus){
    this.dict = dict;   
    this.port = 47777;
    this.plusminus = plusminus;
    }
      public void run() { 

        ServerSocket server;

        boolean isopen = false;
        while (isopen == false){
        try {
            System.out.println(port);
            server = new ServerSocket(port);
            Socket cSocket = server.accept();
            msg = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
            isopen = true;

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            System.out.println("Port is already in used trying next one");
            port = port +1;

            }
        }
        System.err.println(port);
        int i = 0;
        String s = "word";
      try {
        while ((word = msg.readLine())!= null)
         // while ( i < 3)
          {
          //System.out.println("prozess started und das Vorzeichen ist "+plusminus);
         // System.out.println("das Wort "+ word +" ist hier angekommen");
           try { 
           while(true) { // blunt sequential dictionary search


               String line = dict.readLine();
               //System.out.println(line);
               if(line == null){
                   break;  
               }
               else if(word.compareTo(line) < 0)
               {
                 if (plusminus.equals("-"))
                         {
                     System.out.println("Dieses Wort kommt im Dictionary nicht vor " + word);
                         }

               }
               else if(word.compareTo(line)== 0 ){
                 if (plusminus.equals("+")){
                     System.out.println("Dieses Wort kommt im Dictionary 1234 vor " + word);
                 }
                 break;

               }

               else /* word.compareTo(line) > 0 */ continue;  }
           } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           finally{
           }

           }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
user2359459
  • 212
  • 2
  • 11
  • Please provide an example of text file. Is it possible that you read the `\n` (the new line char) with the word? – MeNa Dec 06 '13 at 11:49
  • text file looks something like this Oh Herr, bitte gib mir meine Sprache zurück, ich sehne mich nach Frieden und nem kleinen Stückchen Glück. Lass uns noch ein Wort verstehn in dieser schweren Zeit, öffne unsre Herzen mach die Hirne weit. beautiful german language ^^ – user2359459 Dec 06 '13 at 11:58
  • One Problem certainly is that after the dictionary is read once .. for the next steps its allways null so for the next words. How can i reset dict.readline back to the first item in the dictionary – user2359459 Dec 06 '13 at 12:08
  • Try print the content of `tokenizer.nextToken().toLowerCase().toString()` to your screen and check whether the special german characters doesn't changed to regular english characters. – MeNa Dec 06 '13 at 12:24
  • About the dictionary: this is `inputStream`, the data was stream out. you need put the data in array, or open new stream, or open `RandomAccessFile`. see here: http://stackoverflow.com/questions/262618/java-bufferedreader-back-to-the-top-of-a-text-file – MeNa Dec 06 '13 at 12:35
  • The Problem was non of the above. It was that i did not keep on reading the dict file after the first while loop it was at its end and stoped. I changed the whole thing and used a list to get all the words from the dictionary and then checked with list.contains(word) if the word was in the dictionary or not. thanks for the Help !! – user2359459 Dec 06 '13 at 12:54

1 Answers1

1

text1 = text1 + line; You don't use a space or a delimiter in here. Can that be the problem? )(What I mean is that you print several words, like CatHouseDogFood which isn't a word in the dictionary). It would make sense why it works when you only print one word (i.e. "make")

What do you get if you print the compareTo result at server side?

stevietheTV
  • 502
  • 6
  • 14