I need to put the number 9 in front of the phone if this only with 8 numbers, if it has 9 numbers, I do not need to add the 9

I make I code, but, It's not working!

My user need to be with a total of 9 number- Like: XXXXX-XXXX

If the number has a total of 8 numbers in the phone book- Like this: XXXX-XXXX I need to put the number "9" in front of all numbers, like this: 9XXXX-XXXX

I'm a beginner, can anyone help me? Thank you!

This is a part of my code

   if (phoneNumber != null && !phoneNumber.equals(FireManager.getPhoneNumber())) {
                        //check if contact has installed this app
                        // Is Brazilian number?
                        if (isBrazilPhone(phoneNumber)) {
                            String numberWith9OrNo = phoneNumber;
                            if (phoneHasDigitNine(phoneNumber)) {
                                numberWith9OrNo = new StringBuilder(phoneNumber).replace(5, 6, "").toString();
                                //Log.e("MIGUEL_TAG", "NUMBER WITH A 9 ->" + phoneNumber);
                            } else {
                                // add the 9 digite to find it too
                                numberWith9OrNo = new StringBuilder(phoneNumber).insert(5, "9").toString();
                                //Log.e("MIGUEL_TAG", "NUBER WITHOUT A 9 ->" + numberWith9OrNo);
                            }
                            // Query By numeber with and Withou 9
                            isAFireNumber(context, numberWith9OrNo, id, lastItemId, onContactSyncFinished);
                        }
                        // run with the opposite number from phonenumber.
                        isAFireNumber(context, phoneNumber, id, lastItemId, onContactSyncFinished);
                    }
                }

More:

   public static boolean isBrazilPhone(String formatedPhone) {
        // For BR's Phone
        if (formatedPhone == null || formatedPhone.isEmpty() || formatedPhone.length() <= 7 || !formatedPhone.startsWith("+55")) {
            return false;
        }
        return true;

    }

    public static boolean phoneHasDigitNine(String formatedPhone) {
        if (formatedPhone != null && !formatedPhone.isEmpty() && formatedPhone.length() > 6) {
            //Log.e("MIGUEL_LOG", "NUMERO -> " + formatedPhone);
            // +55089999028370
            if (formatedPhone.substring(3).equalsIgnoreCase("0")) {
                return formatedPhone.substring(6, 7).equalsIgnoreCase("9") && formatedPhone.length() >= 15;
            }
            return formatedPhone.substring(5, 6).equalsIgnoreCase("9") && formatedPhone.length() >= 14;
        }
        return false;
    }

2 Answers

0
Tim Biegeleisen On

I would use a regex replace all here:

String phoneNumber = "1234-5678";
phoneNumber = phoneNumber.replaceAll("^\\d{4}-\\d{4}", "9$0");
if (phoneNumber.matches("\\d{5}-\\d{4}")) {
    System.out.println("MATCH: " + phoneNumber);
}

The logic is:

  • Phone numbers in the format XXXX-XXXX would have a 9 prepended to them
  • Phone numbers with XXXXX-XXXX would not be altered
  • Only the two formats would result in a match
0
Angel Koh On

just remove all non digits and count the length. if you got 8 then add the extra digit in

    String testNumber = "93-21-21-22"
    String filteredNumber = testNumber.replaceAll("[^0-9]","");
    System.out.print(filteredNumber);

    if(filteredNumber.length() ==8){
         testNumber = "9" + testNumber; //add the extra digit if its length is 8.
    }