-2

UPDATE: Thank you all for your answers, especially pertaining to .equals().

The only detail is that the "msgCode = ..." and "msgValue = ..." statements are enough to return an empty stirngBuilder. I.e., I don't even have to declare the IF statement to make it stop working.

Any clues?

ORIGINAL: Please let me know why StringBuilder returns nothing (perhaps doesn't even work) when I include the rest of the code (besides stringBuilder.append(...)) inside the while().

When I include just stringBuilder.append(...), then there is a return value.

while ( (receiveString = bufferedReader.readLine()) != null ) {
    stringBuilder.append(receiveString + "\n");

    // analyze the first 3 characters in the message
    String msgCode = receiveString.substring(0, 3);
    Number msgValue =  Integer.parseInt(receiveString.substring(4, receiveString.length()-4));

    // use IF-ELSE since SWITCH doesn't work with String
    if (msgCode=="ATT") {
        dataATT[2*dataATTcount+1] = msgValue; 
        dataATTcount++;
    } else {
        dataMED[2*dataMEDcount+1] = msgValue; 
        dataMEDcount++;                     
    }
}

Thanks

buped82
  • 51
  • 3
  • check the answer...http://stackoverflow.com/a/513839/2764279 – earthmover Mar 11 '14 at 11:31
  • There is no 'if inside BufferedReader.readLine()' here. – user207421 Mar 11 '14 at 11:39
  • Thank you all for your answers, especially pertaining to .equals(). The only detail is that the "msgCode = ..." and "msgValue = ..." statements are enough to return an empty stirngBuilder. I.e., I don't even have to declare the IF statement to make it stop working. Any clues? – buped82 Mar 11 '14 at 19:36

2 Answers2

0

use

msgCode.equals("ATT")

instead of

msgCode=="ATT"

with '==' you compare the references of the string and not the string itself.

MemLeak
  • 4,456
  • 4
  • 45
  • 84
0

Never-ever use == for Java string comparison. Use equals().

P.S. Okay, there might be some cases where == would be suitable. But for the vast majority of situations, equals() is the way to go.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45