Comparing two EditText values in android - android

I'm trying to make an app where the user enters a word into an EditText box. Then, they enter something into another box and it checks to see if they are the same word. Here's the code that I used:
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
textfield2.setText(textfield2.getText().toString());
if(word == answer){
Toast.makeText(getApplicationContext(), "correct",
Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}
However, it always says that the two strings aren't the same even if they are. Is there a way to fix this?

You can't compare strings with the == operator.
Use .equals() instead:
if(word.equals(answer)) {
//do whatever
}

Use String.equalsIgnoreCase for comparing content of both string variables.:
if(word.equalsIgnoreCase(answer)){
}

Use:
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
if(answer.contentEquals(word)){
// Do something if equals
}
else{
// Do something if not equals
}

I think the best way to do this is using TextUtils:
if(TextUtils.equals(textfield1.getText(),textfield2.getText())){
//do something
}

instead of
if(word.contentEquals(answer)){
}
Use
if(word.equals(answer))
as we cant compare strings with Equal to (==) operator
Try This::
String word = textfield1.getText().toString();
String answer = textfield2.getText().toString();
if(word.equals(answer)){
Toast.makeText(getApplicationContext(), "correct",
Toast.LENGTH_LONG).show();
}else
Toast.makeText(getApplicationContext(), "incorrect", Toast.LENGTH_LONG).show();
}

Related

Use Edit Text for password change

I'm new android developer and i want to check is two Edit Text match with each other or not ,I want it for password change.
here is my code [edited] :
String ChPassword1=ChangePassword1_Box.getText().toString();
String ChPassword2=ChangePassword2_Box.getText().toString();
if(ChPassword1==ChPassword2){
savePreferences("PASSWORD", ChPassword1);
Toast msg = Toast.makeText(getBaseContext(),"رمز تغییر کرد", Toast.LENGTH_LONG);
msg.show();
}
but it doesn't work !!
use the equals method from String to compare strings
if (ChPassword1.equals(ChPassword2)) {
}
== compares strings reference
Better use
ChPassword1.equals(ChPassword2)
Use equals for String matching
if(ChPassword1.equals(ChPassword2)

android: check if the string not only white spaces

How can I check if a string contains anything other than whitespace?
This code didn't work:
String string = " \n\n\t\t ";
if(string.length()==0) doSomething();
since spaces and new lines have values.
Can anyone tell me how can I do it?
Note: minSDKVersion = 5
Regards :)
Try this:
if (string.trim().length() == 0) { /* all white space */ }
Alternatively, you can use a regular expression:
if (string.matches("\\w*")) { . . . }
try:
if (string == null || TextUtils.isEmpty(string.trim()) doSomething();
You can use trim to remove whitespace from both ends of the string. Then compare with the empty string.
Kotlin:
The below code takes the string, trims all of the letters down, and checks to see if the result is white space by using .isEmpty().
val str = " "
if (str.trim().isEmpty()) {
// white space only
} else {
// this string has actual characters/letters in it
}
Try: it works
if (txt.getText().toString().trim().matches(" ")){
Toast.makeText(getApplicationContext(), "You did not select any text"
, Toast.LENGTH_LONG).show();
}
else{
}

Using Comparing number and string in android

Hi to all android developer,
Can you help me about using comparing wildcard character in android.
Example:
Numbers
if( 6.7890666 = 6.789* ){
"Match"
}else{
"Not Macth"
}
string
if( "compare" = "com????" ){
"Match"
}else{
"Not Macth"
}
Can you help how code this in android program
For comparing strings you must use equals.
e.g.
String a = "abcd";
if(a.equals("com"))
{
}
if it is a number do it like this,
if(123 == 123)
if it is a string do it like this,
if(abc.equals(abc))
You can also use
String a = "compare";
if( a.equalsIgnoreCase("com???"))
{
"Match"
}else
{
"Not Macth"
}
equalsIgnoreCase function is not case sencitive.

android EditText value

I have the weirdest problem...
all I am trying to do is to get the value from a EditText and do some validation.
The value in the edittext must be between 1 and 10. However, even if I enter any number between 1 or 10 , it still validates false. I even tested the edittext input to make sure it is correct , and it is, but the if still fails . Any ideas ?
here is the code:
ed = (EditText) dialog2.findViewById(R.id.ed_quantity);
Button bq = (Button) dialog2.findViewById(R.id.alert_a);
dialog2.setCancelable(false);
dialog2.show();
bq.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
String test = ed.getText().toString();
Toast toast23452234 = Toast.makeText(mContext, "Quantity: "+test, Toast.LENGTH_LONG);
toast23452234.show();
if(test=="1"||test=="2"||test=="3"||test=="4"||test=="5"||test=="6"||test=="7"||test=="8"||test=="9"||test=="10")
{
quantity = Integer.parseInt(ed.getText().toString());
dialog2.dismiss();
ed.setText("1");
}
else
{
Toast toast2345223 = Toast.makeText(mContext, "Quantity must be between 1 and 10" , Toast.LENGTH_LONG);
toast2345223.show();
}
}
});
try this
test.trim().equalsIgnoreCase("1")
in your if-condition
Use equals method to compare strings..
if(test.equals("1")||test.equals("2")||test.equals("3")||test.equals("4")||test.equals("5")||test.equals("6")||test.equals("7")||test.equals("8")||test.equals("9")||test.equals("10"))
or use int to compare
int test = Integer.valueOf(ed.getText().toString());
String is not a native type so you can't do test=="1". This compares the object references and obviously the two objects have different references.
Call equals(Object object) method on string object. Like,
test.isEquals("1")
Better parse the input string to integer as
int testInteger = Integer.parseInt(test);
and compare as
if(testInteger==1 || testInteger ==2)
This saves many method calls to string object.
use .equals("") in case of String.
if(test.equals("1")||test.equals("2")||test.equals("3")||test.equals("4")||test.equals("5")||test.equals("6")||test.equals("7")||test.equals("8")||test.equals("9")||test.equals("10"))
use .equals on string operation instead of ==
if(test.equals("1")||... and so on)
or else convert string into "int".
Use test.equals("1") || .....
You can also do this
int t = Integer.parseInt(test);
if(t == 1 || t == 2 || ...)

validating EditText input

Trying to confirm that an EditText tool is not empty when the user clicks a button. Whenever the EditText is not blank the code works. But whenever it is blank my program crashes. Any ideas what I'm doing wrong? Thanks.
if (et1.getText().toString() != null) {
inpt_weight = Integer.parseInt(et1.getText().toString());
Toast.makeText(getBaseContext(), "weight is not null", Toast.LENGTH_LONG).show();
} else {
inpt_weight = 0;
Toast.makeText(getBaseContext(), "weight is null", Toast.LENGTH_LONG).show();
};
I think its trying to parse an empty string("") to an integer which is not possible
try:
if (!et1.getText().toString().equalsIgnoreCase("") && et1.getText().toString() != null)
This will be a good way:
if(et1.getText().toString().trim().length()==0){
Log.d("No data","No text found in the edit text");
}
This also checks if the user had put nothing but only spaces.
In your code, I see you're converting the text to numbers.
In this case, either put correct IMEMethod for the edit text so that it takes only numbers, or check in your code that user has entered only numeric characters, else you'll end up with a NumberFormatException while conversion if the user has entered non numeric characters.
Try the following code please..
if(et1.getText().toString().equals("")){
Toast.makeText(getApplication(), "weight is null", Toast.LENGTH_SHORT).show();}
else{
inpt_weight = Integer.parseInt(et1.getText().toString());
Toast.makeText(getApplication(), "weight is not null & it is : "+inpt_weight,Toast.LENGTH_SHORT).show();
}

Categories

Resources