there are some problem in result of if condition that check the text of textView
this is the code for more explain
times.xml
<TextView
android:id="#+id/tiMinutesView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="--"
android:textColor="#3134E8" />
TimesActivity.java
TextView minView;
minView = (TextView) v.findViewById(R.id.tiMinutesView);
if (minView.getText().toString().trim() != "--") {
Toast.makeText(TimesActivity.this,
"OK",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(TimesActivity.this,
"NO", Toast.LENGTH_LONG)
.show();
}
Now it is supposed to return false and display NO because it is equal "--" actually, but it returns true and display OK
What is the problem ???
Always use equals() method for String value comparison.
if (!minView.getText().toString().trim().equals("--")) {
....
}
== is used for comparing the object references. equals() is the one used for value comparisons.
P.S:- There are like a zillion questions here # SO, which explain this in more detail. Do refer them for more clarity.
You're on Java dude, so change the relevant line to:
if (minView.getText().toString().trim().equals("--"))
{
//write your code here.
}...
always compare string using equals() of the string value/object.
if(!minView.getText().toString().trim().equals("--")){
}....
this will compare the value and minView.getText().toString().trim() != "--" this will compare the two string object
use this
if (!minView.getText().toString().trim().equals("--")
instread of using
if (minView.getText().toString().trim() != "--")
Related
I am trying to ignore spaces in editview between text, I am not quite sure how I can go about doing this. I know I can use trim feature to ignore spaces before and after the full text but how do I ignore space between strings if there is any;
String myTextEdited myText.getText().toString().trim();
For example, if I have / user types in this;
Allan Bob
3523 JKO
NY1 U90
I want to ingore spaces when I read this in my if statement or put it in another variable for example;
String name = "AllanBob"
For example, to ignore upper and lower cases I am doing this;
if (myText.getText().toString().trim().equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
What I would like to do is add another feature in here that also ignores spaces before, between and after text e.g. instead of;
myname is Henry . (space until here)
It should read it as mynameishenry but to the user it still appears as they have written it.
Please let me know if my question was not clear, I will try explaining it better
EDITED:
is it possible to ignore spaces in string that I have inside my if statement. For example;
if (myText.getText().toString().trim().equalsIgnoreCase("Henry 0887")) {
// do something
} else {
// do something
}
but currently if the user types in henry0887, the if statement does not validate it because I added a space inside my validation text and therefoe its looking for a space in the text, is it possible to over come this, so even if I have space inside my validation it ignores it.
Did you try this:
String myString = myEditText.getText().toString();
myString = myString .replace(" ", "");
Hope it helps
EDIT:
if (myText.getText().toString().replace(" ", "").equalsIgnoreCase(userInput) || myText.getText().toString().equalsIgnoreCase(userInput)) {...
Try this,
if(myText.getText().toString().trim().replace(" ","").equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
Hope this helps.
use replaceAll() method.
str = str.replace(" ","");
or for all space chars:
str = str.replace("\\s+","");
EDIT
if (myText.getText().toString().replace("\\s+","").equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
EDIT2
if (myText.getText().toString().replace("\\s+","").equalsIgnoreCase("Henry 0887".replace("\\s+",""))) {
// do something
} else {
// do something
}
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();
}
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)
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{
}
I have a strange problem in my android app. I must compare two string which are equals. I tried this :
if (raspunsdata.equals(rok)) {
System.out.println("changed ");
} else
System.out.println("no change");
}
but I get always "no change". Before this I have System.out.println for both strings, and both of them have the same value.
I tried also (raspunsdata==rok) and raspunsdata.contentEquals(rok) but I have the same problem. Why? I cant understand this.,...please help...
You might have unwanted white spaces. Might need to use the trim function just to make sure.
if (raspunsdata.trim.equals(rok.trim())) {
System.out.println("changed ");
} else
System.out.println("no change");
}
Btw equals is the correct way to check whether the values are the same.
.equals - compares the values of both objects. If you have 2 Strings with the same characters sets .equals will return true;
== - compares if two objects references are equal.
For example:
String a = "lol";
String b = a;
a == b - will be true.
Try reading: http://www.devdaily.com/java/edu/qanda/pjqa00001.shtml