-3

What is wrong with the code:

String maintext = (String) main_text.getText().toString();

if(maintext =="10") {           
    ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
    toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
}

The code is working without if statement but with the use of if and .getText().toString() is not working at all.

krossovochkin
  • 12,030
  • 7
  • 31
  • 54
Usman Ahmed
  • 79
  • 2
  • 3
  • 11

2 Answers2

1

== tests object references, .equals() tests the string values.

use equals

if(maintext.equals("10"))

Finally

    if(maintext.equals("10"))
    {

        ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
        toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);


   }

How do I compare strings in Java?

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

You are incorrectly comparing two strings (maintext =="10"), change it to "10".equals(maintext)

Johan
  • 8,068
  • 1
  • 33
  • 46
Shadow Droid
  • 1,696
  • 1
  • 12
  • 26