Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,804,274 Members 38,102 Now Online
XDA Developers Android and Mobile Development Forum

[Q] Need help with a program.

Tip us?
 
SurajKamble
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 3
Join Date: Aug 2014
Default [Q] Need help with a program.

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.
 
panwrona
Old
#2  
Junior Member
Thanks Meter 1
Posts: 12
Join Date: Jun 2014
Location: Wrocław

 
DONATE TO ME
You're passing view argument in function so try initialize edit text with (EditText)view.findViewById(R.id.edittext);
 
SurajKamble
Old
#3  
Junior Member - OP
Thanks Meter 0
Posts: 3
Join Date: Aug 2014
Quote:
Originally Posted by panwrona View Post
You're passing view argument in function so try initialize edit text with (EditText)view.findViewById(R.id.edittext);
Thanks for the reply.
I did what you said and got a runtime exception.
 
panwrona
Old
#4  
Junior Member
Thanks Meter 1
Posts: 12
Join Date: Jun 2014
Location: Wrocław

 
DONATE TO ME
You're running it in fragment or activity?
 
SurajKamble
Old
#5  
Junior Member - OP
Thanks Meter 0
Posts: 3
Join Date: Aug 2014
Quote:
Originally Posted by panwrona View Post
You're running it in fragment or activity?
Activity.
 
panwrona
Old
#6  
Junior Member
Thanks Meter 1
Posts: 12
Join Date: Jun 2014
Location: Wrocław

 
DONATE TO ME
Are you initializing it in oncreate or somewhere else?
 
arpitkh96
Old
#7  
arpitkh96's Avatar
Senior Member
Thanks Meter 384
Posts: 1,093
Join Date: Feb 2012
Location: panipat
That's easy. Instead of == use text.equals(one)
String are not compared by mathematical signs

Sent from my XT1033 using XDA Premium 4 mobile app

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes