3

im trying to do this if statement to give me true if i say the word "hi" ,, but it gives me false aalll the time!! can someone please tell me why?! here is my code:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (requestCode ==check && resultCode == RESULT_OK){
        ArrayList<String> results    =data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        lv.setAdapter(new ArrayAdapter <String>(this   ,android.R.layout.simple_list_item_1,results));
        TextView display=(TextView)findViewById (R.id.TOF);

        String what_you_say = lv.getContext().toString();
        if (what_you_say.contentEquals("hi") == true)

            display.setText("True");

        else

            display.setText("false");       
    }
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 2
    What you have, should work assuming case/whitespace matches. For debugging you should do a println() or log on what_you_say for sanity. Or breakpoint if you're using an IDE. – Blaine Mucklow Feb 28 '13 at 21:20
  • Keep in mind that its typically preferred that a constant string declared as final in your application. This makes it easier to change in the future and also limits the change to only one place instead of n Places. – JoxTraex Feb 28 '13 at 21:31

3 Answers3

0

what_you_say.equals("hi") should work. Also it's a good practice to not assume the casing on something you check (unless you want case specific). So you should do what_you_say.equalsIgnoreCase("hi")

Blaine Mucklow
  • 592
  • 7
  • 18
0

Replace this line:

 if (what_you_say.contentEquals("hi") == true)

With this other line:

 if (what_you_say != null && what_you_say.trim().equalsIgnoreCase("hi"))

In this case, you're interested in simple string equality, but it's a good idea to take some extra precautions: considering the case where the string is null, removing extra spaces at the beginning and end, and ignoring case will usually help. To see the difference with content equality, see this post.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • if `what_you_say.equals("hi")` return true then `what_you_say.contentEquals("hi")` should return true as well. – PermGenError Feb 28 '13 at 21:12
  • 1
    @ايمانمحمد Then do this right before the `if`: `System.out.println("input:" + what_you_say);` and check the output in the console. Surely, the string does NOT contain the text "hi", it's the only possible explanation. – Óscar López Feb 28 '13 at 21:45
0

You should change your if-statement as follows:

if ("hi".equals(what_you_say))

or

if ("hi".equalsIgnoreCase(what_you_say))

Depending on whether or not you need the check to be case-sensitive.

You should always do your String comparisons in this manner, because it will be null-safe.

Take the following code snippets:

if ("hi".equals(what_you_say)) // what_you_say is null will evaluate to false

if (what_you_say.equals("hi")) // what_you_say is null will cause NullPointerException

It's better for the if-statement will evaluate to false instead of causing a NullPointerException.

Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
  • 1
    While I wouldn't necessarily disagree with this "answer", it doesn't actually answer the question. This should probably be a comment. – kabuko Feb 28 '13 at 21:30
  • Thanks everyone for you answers,, but the problem turned out to be in defining the variable "what_you_say" – ايمان محمد Mar 03 '13 at 20:15