0

BluetoothTest.java

colourSpinner = (Spinner) findViewById(R.id.colourSpinner);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
            R.array.colour, android.R.layout.simple_spinner_item);
    colourSpinner.setAdapter(adapter);

-

-

-

void redButton() throws IOException {
    if(colourSpinner.getSelectedItem().toString() == "Red")
    {
        Toast.makeText(getApplicationContext(), 
        colourSpinner.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
        mmOutputStream.write("1".getBytes());
    }
}

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Computer Case Controller</string>
<string-array name="colour">
    <item>Red</item>
    <item>Green</item>
    <item>Blue</item>
    <item>Cyan</item>
    <item>Magenta</item>
    <item>Yellow</item>
    <item>White</item>
</string-array>

Hey I have a problem that I've been trying to solve for a while now, I don't understand why my code doesn't work as intended.

Basically redButton() sends the command "1" over bluetooth, while also creating a toast saying the colour of the Spinner selection. I've narrowed it down to the if statement not working, but I don't understand why colourSpinner.getSelectedItem().toString() doesn't equal "Red" even if that is what it outputs if I set it to toast the colour without the if statement

Am I missing something?

  • [How do I compare strings in java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Gabriel Apr 25 '14 at 18:18

2 Answers2

2

You need to do .equals on the string operator, not ==.

if(colourSpinner.getSelectedItem().toString().equals("Red"))

This question gets asked quite a bit (with Strings in particular), and I believe the reason why to be fairly simple. We are able to get away with the mistake on a fairly regular basis, so when the issue arises it is hard to catch that it isn't working.

String string1 = "hello";
String string2 = "hello";
if(string1 == string2)
    System.out.println("We have a match");

This will print the output, as the equality is correct. That is because our values get dumped into a heap pool when we create the literal String constant, "hello". At this point, any time we create a string and set it equal to the heap constant, it will reference that value in the heap. The problem is that sometimes the Strings we need to use aren't instantiated in such a direct way. Sometimes, depending on the way the program is designed, Strings may be assigned as a new value in memory, even if there is a matching constant in our heap. For example,

String string1 = "hello";
String string2 = new String("hello");
String string3 = new String("hello");
if(string1 == string2 || string2 == string3)
    System.out.println("We have a match");

We will not hit our print to output in this example, as our reference to our heap constant "hello" is not the same as our reference to our newly created "hello" Strings in memory. In simpler programs, we often set our own strings so the == comparison will work, but as things get more complex, specifically when we ask Android to do things for us, that assumption is no longer valid and the .equals comparison is necessary.

There is more about this concept here:

http://www.thejavageek.com/2013/07/27/string-comparison-with-equals-and-assignment-operator/

zgc7009
  • 3,371
  • 5
  • 22
  • 34
1

You seem to rely on the caching of Strings by Java, i.e. always getting the same String Object for identical characters. However if you take a look at this question you'll find out that this is only guaranteed to be the case if you use String literals everywhere. So if you do not know or cannot guarantee that the String was created through a literal, use equals() for comparison.

Community
  • 1
  • 1
orbdan
  • 31
  • 2