Home > front end >  Why am I not able to assign the correct value of boolean result found in the program, to a boolean v
Why am I not able to assign the correct value of boolean result found in the program, to a boolean v

Time:12-11

I am receiving the value as true when log.d ing the logcat, but when I assign the value to boolean variable it changes to false and also I am not able to do the if condition with the value as it also is showing false. That is I am only able to get the value as true in log, don't know how to take the value to a variable or check some conditions. This code is used to validate a phone number:

Log.d("testi3", "id "   edit_number.getText());

    if (number.isEmpty()) {
        Log.d("testi3", "id empty"   edit_number.getText());
        edit_number.setError(getResources().getString(R.string.phone_error));
        isPhoneValid = false;
    } else {
        Log.d("testi3", "id else "   edit_number.getText());

        Pattern ptrn = Pattern.compile("[7-9][0-9]{9}");

        Matcher match = ptrn.matcher(number);

        Log.d("testi3", "id else 1 "   (match.find() && match.group().equals(number)));
        isPhoneValid = (match.find() && match.group().equals(number));
        Log.d("testi3", "id else validi"   isPhoneValid);

        if ((match.find() && match.group().equals(number))) {
            Log.d("testi3", "id else 2 "   (match.find() && match.group().equals(number)));
            isPhoneValid = true;
        } else {
            Log.d("testi3", "id else 3 "   (match.find() && match.group().equals(number)));
            edit_number.setError(getResources().getString(R.string.phone_error));
            isPhoneValid = false;
        }
    }

This is the logcat on entering a number as input:

D/testi3: id 9048857563
D/testi3: id else 9048857563
D/testi3: id else 1 true
D/testi3: id else validi false
D/testi3: id else 3 false

CodePudding user response:

A matcher is created from a pattern by invoking the pattern's matcher method. Once created, a matcher can be used to perform three different kinds of match operations:

  • The matches method attempts to match the entire input sequence against the pattern.
  • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.
  • The find method scans the input sequence looking for the next subsequence that matches the pattern.

Each of these methods returns a boolean indicating success or failure. More information about a successful match can be obtained by querying the state of the matcher.

Matches - Attempts to match the entire region against the pattern. If the match succeeds then more information can be obtained via the start, end, and group methods.
In your scenario , when you first time matches the patter using (match.find() && match.group().equals(number))) - match is found success and values in the matcher are updated, to be more specific matchFound and group value is updated. Next time comparing the values using same matcher group doesn't satisfy the condition and hence result in false.

You can use match.matches() to check if match is success or not for next use of match instead of comparing again.
Or you can reset the matcher using reset() method

  • Related