borneo
borneo

Reputation: 97

How to compare Long type data in android studio

I want to compare IMEI number in my phone with the data I declare, but the bug says: 'Operator == can not be applied to 'java.lang.String', 'long' How do I fix that?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TelephonyManager manager;
    private Button button;
    TextView textView;
    String IMEI;
    long IMEI2 = 356261058647361;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.next);
        textView = (TextView)findViewById(R.id.textview1);

        manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        IMEI = manager.getDeviceId();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(IMEI == IMEI2){
                    textView.setText("IMEI NUMBER : " + IMEI);

                }else{
                    textView.setText("data IMEI and IMEI2 did not match");
                }

            }
        });
    }

}

Upvotes: 4

Views: 1529

Answers (4)

Varad Mondkar
Varad Mondkar

Reputation: 1561

Use Datatype Long instead of long. Always use L at the end of your value while initiating Long/long datatype and check it using equals

Long IMEI;
Long IMEI2 = 356261058647361L;

IMEI = Long.valueOf(manager.getDeviceId());

if (IMEI2.equals(IMEI)) {
    textView.setText("IMEI NUMBER : " + IMEI);
} else {
    textView.setText("data IMEI and IMEI2 did not match");
}

Upvotes: 3

Gowthaman M
Gowthaman M

Reputation: 8282

Try this

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(IMEI2.equals(""+IMEI){
                    textView.setText("IMEI NUMBER : " + IMEI);

                }else{
                    textView.setText("data IMEI and IMEI2 did not match");
                }

            }
        });

Upvotes: 3

Vidhi Dave
Vidhi Dave

Reputation: 5684

In java for long you need to declare like long IMEI2 = 356261058647361L;

you need to add 'L' . refere : Long type

Long.parseLong()

The Long.parseLong() static method parses the string argument as a signed decimal long and returns a long value.

Use :

Long.parseLong(IMEI);

Use this way : long tmp = Long.parseLong(IMEI); before the if condition.

then in if :

if (tmp == IMEI2)

Reference: Long.parseLong()

Upvotes: 2

Rajan Kali
Rajan Kali

Reputation: 12953

if(IMEI.equals(String.valueOf(IMEI2)){
    textView.setText("IMEI NUMBER : " + IMEI);
}else{
    textView.setText("data IMEI and IMEI2 did not match");
}

Upvotes: 3

Related Questions