-2

I'm trying to get text from server and then check it a to know what actions to take with the text adopted. The problem is that when I try to check if the received text for example is "Exited" the query always return the value "false" when the received text is really "Exited".

Here is the code :

class Get_Message_From_Server implements Runnable
   {
    public void run()
     {    
      InputStream iStream = null;
      try
       {
        iStream = Duplex_Socket_Acceptor.getInputStream();
       }
      catch (IOException e)
       {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      //Create byte array of size image
      byte[] Reading_Buffer = null;

      try
       {
          Reading_Buffer = new byte [Duplex_Socket_Acceptor.getReceiveBufferSize()];
      //New_Buffer = new byte [100];
       }
      catch (IOException e1)
       {
        // TODO Auto-generated catch block
        e1.printStackTrace();
       }

      byte[] Byte_Char_1 = new byte[1];

      int Byte_String_Lenght = 0;

      //read size
      try
       {
        iStream.read(Reading_Buffer);

        String Reading_Buffer_Stream_Lenghtor = new String(Reading_Buffer);

        //System.out.println("full : " + Reading_Buffer_Stream_Lenghtor);

        Byte_String_Lenght = Reading_Buffer_Stream_Lenghtor.indexOf(new String(Byte_Char_1));
       }
      catch (IOException e)
       {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      //Convert to String
      Meassage = new String(Reading_Buffer);

      Meassage = Meassage.substring(0, Byte_String_Lenght);//The text that received

      Message_Getted = 1;
    }
  }

The query :

if(Message_1 != "Exited")//the message query
  {
   System.out.println("Continued 253");
   continue;
  }

Its always return the value - false

  • its important to know that the message is in Utf - 8 encoding

so how i can to fix the issue ?

anri97
  • 11
  • 1
  • 8

2 Answers2

0

If you compare strings by using oparators, Java will not look at the contents of the string but at the reference in memory. To compare String content in Java, you should use the following:

String Message_1; // Hopefully has a value sent by the server
if(Message_1.equals("Exited")) {
    // Do stuff when exited
} else {
    // Do stuff when not exited
}
Smokez
  • 382
  • 1
  • 5
0

String is a variable - and variables should start with lower Case letter - Please read Java Code conventions. Also to check if your message contains string you thing it should just do System.out.println(Message_1); and if the message contains what you expect you compare string doing

if(Message_1.equals("Exited")) {
   System.out.println("Yes they are equal");
} else {
   System.out.println("No they are not");
}

If this will print "No they are not" that simply means that your variable Message_1 is not what you think it is.. As simple as that. There is no such a thing as .equals method does not work. Its your variable that doesn't ;)

Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72