0

I am using Eclipse to run the following code.

I am trying to compare what I'm entering in the TextView to what I have in my string array. I even went as far as to set the TextViews text and the string arrays value that I'm comparing to the same thing before the if statement runs. I feel as though I'm missing something simple here so maybe someone could point me in the right direction. Here is some sample code:

public class StartActivity extends Activity {

String[] keyWord = {"cow", "hour", "trial", "active", "purpose", "cat", "route", "laser", "fresh", "success" };

Random randWord = new Random();
int secretWord = randWord.nextInt(10);

This is where I am comparing the values

public void SubmitClick(View v){
    TextView TV = (TextView)findViewById(R.id.txtAnswer);
    keyWord[secretWord] = "the";
    TV.setText("the");
    if(keyWord[secretWord] == TV.getText().toString()){

    Toast.makeText(getApplicationContext(), "Correct guess!",
               Toast.LENGTH_LONG).show();
    }
}

When I select the submit button on my device the code never runs. I tried adding an else in there which is what I wan't and it always comes back as not equal so I have no idea what I'm missing here.

Thanks

Charles
  • 5
  • 3
  • Please search SO. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – pmac89 Apr 05 '14 at 19:59

1 Answers1

0

You must compare Strings with equals() method, not with ==.

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
  • If you provide answer remember to beside solution include explanation of problem. Why using `equals` is correct approach, while `==` is not? – Pshemo Apr 05 '14 at 20:07
  • I suppose user3064123 will understand that on his own by checking the links that were already provided. I was just providing a quick answer. – joao2fast4u Apr 05 '14 at 20:16
  • 1
    Thanks! that was definitely it. I let the c# get the best of me ;) – Charles Apr 05 '14 at 20:19