F
F
Fedor unknown2021-11-19 11:58:51
Java
Fedor unknown, 2021-11-19 11:58:51

How to change the contents of an ArrayList?

I have a list of phone numbers, I need to convert them to a certain format, but my code doesn't work.
what could be the problem?

public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

        List<String> number = new ArrayList<>();
        number.add("+996555691100");
        number.add("996555691100");
        number.add("0555691100");
        number.add("555691100");
        number.add("+79259243307");
        number.add("0079259243307");
        number.add("89259243307");
        number.add("+77077496188");
        number.add("0077077496188");
        number.add("87077496188");

                number.stream().map(s -> s = fix_phone(s)).collect(Collectors.toList());

    }

    public String fix_phone(String phone) {

        // Убрать все кроме цифр
        phone = phone.replaceAll("[^\\d.]", "");

        //Если номер  начинается с 0, то удалить 0
        if (phone.startsWith("0996")) phone = phone.replaceAll("^[0]", "");

        // Если номер (РФ) начинается с 00, то удалить: 00
        if (phone.startsWith("00")) phone = phone.replaceAll("^[0][0]", "");

        //Если номер (РФ) начинается с 7, то заменить на 8
        if (phone.startsWith("8")) phone = phone.replaceAll("^[8]", "7");

        // Если номер содержит только 9 цифр, дабавить код страны 996
        if (phone.matches("[\\d]{9}")) phone = "996" + phone;

        // Заменить 0 на 996
        if (phone.matches("0[\\d]{9}")) phone = phone.replaceAll("^[0]", "996");

        return phone;
    }

}


The output is the original sheet without changes, what could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacen11, 2021-11-19
@turdubekov

where is your exit?
well, at least look at what the methods return, they do not work by magic. Study the stream api once you use it
PS conditions and regular expressions are curves, why do you check startsWith if the regular expression can immediately replace zeros at the beginning with emptiness

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question