Search code examples
androidif-statementnfc

if statement doesn't work for string


In my project, i read the text in NFC Tag, and i save it to String value. And if string value == given string , do something.

here is my code :

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                if(nfcTag==null){
                    Toast.makeText(context, "Lütfen NFC Etiketini Okutunuz", Toast.LENGTH_SHORT).show();
                }else{
                    nfcString = read(nfcTag);  // this is the part i get the text with read function. i already define nfcString as String.
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            textViewName.setText(nfcString); // it is working fine !

            String salak = textViewName.getText().toString();
            // i created new String because, i tried to get textViewName.getText().toString() , i tought it should be work, but it didn't 

            try {
                // this is the if statement which doesn't work..
                if ( salak == getName(position) ) {
                    // i also tried 
                    // nfcString  == getName(position)
                    // nfcString.toString()  == getName(position).toString()
                    // position thing is about spinner. i use spinner, it works fine also..

                    btn.setText("Girdin!");
                    yoklamaSend(getDersid(position));

                } else
                    Toast.makeText(context, "DERS GİRDİSİ BAŞARISIZ", Toast.LENGTH_LONG).show();
            }
            catch (Exception ee) {
                ee.printStackTrace();

            }
        }
    });

I can't understand why it is not working. NFC Tag is working fine, but whatever i do, i couldn't run if statement.

I get the NFC Tag Read code from sample of different app (which is working fine), here it is :

    private String read(Tag tag) throws UnsupportedEncodingException {

    Ndef ndef = Ndef.get(tag);
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();
    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                byte[] payload = ndefRecord.getPayload();
                String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
                int languageCodeLength = payload[0] & 0063;
                return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
            } catch (UnsupportedEncodingException e) {
                Log.e("TAG", "Unsupported Encoding", e);
            }
        }
    }
    return null;
}

Solution

  • The correct way to compare strings is with .Equals(String).

    Using the == compares to see if they are the same object in memory

    See these links:

    https://docs.oracle.com/javase/tutorial/java/data/comparestrings.html

    How do I compare strings in Java?

    Java String.equals versus ==