Answer the question
In order to leave comments, you need to log in
Phone number verification with Matcher, Pattern?
It is necessary so that until the user enters the correct number, he will not be able to exit the code block. Is there a way to use the "while" statement, or is there another way to implement it?
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Введите свой моб. телефон. Пример: +380994563477");
String phoneNumber = scan.nextLine();
Pattern pattern = Pattern.compile("^((\\+?+380)([0-9]{9}))$");
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) {
System.out.println("Phone: " + phoneNumber + " is correct!"); //введен корректныый номер
}else {
System.out.println("Phone: " + phoneNumber + " no correct!"); //введен некорректный номер
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question