Need help with my program.

  • Replies:1
Suraj Kamble
  • Forum posts: 1

Aug 31, 2014, 4:39:31 PM via Website

public void click(View view) {
String one = "one";
EditText et = (EditText)findViewById(R.id.editText1);
String entered_text = et.getText().toString();

if(et.getText().toString() == one){
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Correct!";);
}
else{
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(one+entered_text); }
}
This is a code snippet extracted from my program, i didn't post the whole program because it wasn't necessary, as the program runs fine without any runtime exceptions.
So, the program when executed on eclipse doesn't show any errors and runs fine, but when run the "if" condition "et.getText().toString() == "one"" always returns false even when the "entered_text" is "one" i.e.; it never prints "correct!" and the code always prints "one+entered_text" that is the statement in the else clause. And the interesting thing is, if you enter "one" the output will be "oneone", that is the else statement.
Please help me where i went wrong.
Thanks in advance.

Reply
AppBatics
  • Forum posts: 12

Sep 6, 2014, 6:52:17 AM via Website

you have to use String.equals, not ==.

Reply