1
\$\begingroup\$

I need to detect device model, and if it is Galaxy Ace GT-S5830i, print it out. But it never print's it (it doesn't get through condition).

My code:

public String mDeviceModel = android.os.Build.MODEL;

System.out.println(mDeviceModel); //this prints my GT-S5830i

if (mDeviceModel == "GT-S5830i") //compiler don't get thru this cond.
    System.out.println("GALAXY ACE S5830i"); //never printed

I can't get what's wrong here.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

It's just a classic string comparison bug. In java "==" compares the object references to each other to see if they are the same object. In this case the string you just created for the comparison and the string android.os.Build.MODEL are two completely different string/objects even though their content may be the same. To compare the content of two strings, use the strings equals() method.

if (mDeviceModel == "GT-S5830i")
    System.out.println("GALAXY ACE S5830i");

Should read

if (mDeviceModel.equals("GT-S5830i"))
    System.out.println("GALAXY ACE S5830i"); // Should now print.
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .