Usman Ahmed
Usman Ahmed

Reputation: 79

Android editText.getText()

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.

Upvotes: -3

Views: 127

Answers (2)

Shadow Droid
Shadow Droid

Reputation: 1696

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

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

== 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?

Upvotes: 1

Related Questions