Using Comparing number and string in android - 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.

Related

how to check the text in first two buttons equals to the text in third button

Here is my code .This doesn't work
button1.getText() + button2.getText() == button3.getText()
Use equals to compare strings,
Try this,
String button1Text = button1.getText().toString();
String button2Text = button2.getText().toString();
String button3Text = button3.getText().toString();
if((button1Text + button2Text).equals(button3Text)){
// strings are equal
} else {
// strings are not equal
}
button[6].getText().toString().equals(button[0].getText().toString().concat(button[3].getText().toString()));
When you work with Strings in Java the operator == checks if the two objects refer to the same instance of an object.
While equals() checks if the two objects are actually equivalent, even if they are not the same instance.
try:
String button1Text = button1.getText().toString();
String button2Text = button2.getText().toString();
String button3Text = button3.getText().toString();
if (button1Text.equals(button3) && button2Text.equals(button3)) {
// do something...
} else {
// do something...
}

Comparing two EditText values in 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();
}

How to Validate Phone Number format

i am about to create a validation for phone number format..The format is 10 digit including the plus sign eg:+0133999504. Even though I have declare the pattern which is I try to disallow the "-" symbol or any other characters, but the validation is not working. Any other Idea or solution?
1st I declared the string regex:
String PhoneNo;
String PhoneNo_PATTERN ="[\\+]\\d{3}\\d{7}";
2nd I make a if..else statement:
{
Pattern pattern = Pattern.compile(PhoneNo_PATTERN);
Matcher matcher = pattern.matcher(PhoneNo);
if (!matcher.matches())
{
inputemergencyContactNo.setError("Please enter Emergency Contact No");
}
else{
Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
}
Why not remove all non-digits and then count the digits left and put the plus back in later? This allows users the freedom to fill out their phone number anyway they want...
String PhoneNo = "+123-456 7890";
String Regex = "[^\\d]";
String PhoneDigits = PhoneNo.replaceAll(Regex, "");
if (PhoneDigits.length()!=10)
{
// error message
}
else
{
PhoneNo = "+";
PhoneNo = PhoneNo.concat(PhoneDigits); // adding the plus sign
// validation successful
}
If your app is intended for international use replace
if (!PhoneDigits.length()!=10)
with
if(PhoneDigits.length() < 6 || PhoneDigits.length() > 13)
as Fatti Khan suggested.
To apply this in the code you posted at Android EditText Validation and Regex first include this method in your public class or the class containing onClick():
public boolean validateNumber(String S) {
String Regex = "[^\\d]";
String PhoneDigits = S.replaceAll(Regex, "");
return (PhoneDigits.length()!=10);
}
And include this method in the CreateNewRider class:
protected String tidyNumber(String S) {
String Regex = "[^\\d]";
String PhoneDigits = S.replaceAll(Regex, "");
String Plus = "+";
return Plus.concat(PhoneDigits);
}
This is where the validation happens...
#Override
public void onClick(View view) {
Boolean b = false;
if(inputfullname.getText().toString().equals("")) b = true;
else if(... // do this for all fields
else if(inputmobileNo.getText().toString().equals("")) b=true;
else if(inputemergencyContactNo.getText().toString().equals("")) b=true;
else {
if(validateNumber( inputmobileNo.getText().toString() )
Toast.makeText(RiderProfile.this, "Invalid mobile number", Toast.LENGTH_SHORT).show();
else if(validateNumber( inputemergencyContactNo.getText().toString() )
Toast.makeText(RiderProfile.this, "Invalid emergency contact number", Toast.LENGTH_SHORT).show();
else {
// Validation succesful
new CreateNewRider().execute();
}
}
if(b) Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
}
And then use tidyNumber() in the CreateNewRider class:
protected String doInBackground(String... args) {
String fullname= inputfullname.getText().toString();
String IC= inputIC.getText().toString();
String mobileNo= tidyNumber( inputmobileNo.getText().toString() );
String emergencyContactName= inputemergencyContactName.getText().toString() );
String emergencyContactNo= tidyNumber( inputemergencyContactNo.getText().toString() );
...
Given the rules you specified:
upto length 13 and including character + infront.
(and also incorporating the min length of 10 in your code)
You're going to want a regex that looks like this:
^\+[0-9]{10,13}$
With the min and max lengths encoded in the regex, you can drop those conditions from your if() block.
Off topic: I'd suggest that a range of 10 - 13 is too limiting for an international phone number field; you're almost certain to find valid numbers that are both longer and shorter than this. I'd suggest a range of 8 - 20 to be safe.
[EDIT] OP states the above regex doesn't work due to the escape sequence. Not sure why, but an alternative would be:
^[+][0-9]{10,13}$
[EDIT 2] OP now adds that the + sign should be optional. In this case, the regex needs a question mark after the +, so the example above would now look like this:
^[+]?[0-9]{10,13}$
For Valid Mobile You need to consider 7 digit to 13 digit because some country have 7 digit mobile number . Also we can not check like mobile number must starts with 9 or 8 or anything..
For mobile number I used this this Function
private boolean isValidMobile(String phone2)
{
boolean check;
if(phone2.length() < 6 || phone2.length() > 13)
{
check = false;
txtPhone.setError("Not Valid Number");
}
else
{
check = true;
}
return check;
}
^[\\+]\\d{3}\\d{7}$
Use anchors to limit the match.
^ => start of match
$=> end of match
To validate India's mobile number.
Your edit text input
edt_mobile.text.toString().trim()
Number validation method
fun isValidMobile(phone: String): Boolean {
return phone.matches(Constants.REGEX_MOBILE.toRegex()) && phone.trim().length == 10
}
Regression expression
const val REGEX_MOBILE = "^[6-9]{1}[0-9]{9}\$"

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{
}

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 || ...)

Categories

Resources