Validating Phone Numbers in Java with DO WHILE loop


In this post, I am going to write a program for validating phone numbers in java with do while loop.

In many applications, it is necessary to verify that a phone number has a specific number of digits. For example, a phone number in the United States must have 10 digits, including the area code.

Also Read: Print 1 to 50 using do-while loop in Java

In this tutorial, we will look at how to use a do-while loop in Java to check whether a phone number entered by the user is a 10-digit number.

Validating Phone Numbers in Java with DO WHILE loop

import java.util.Scanner;

import java.util.Scanner;

public class PhoneNumberChecker {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(;
    String phoneNumber;
    boolean isValid;

    do {
      System.out.println("Enter a phone number (10 digits): ");
      phoneNumber = scanner.nextLine();

      isValid = phoneNumber.matches("[0-9]{10}");
      if (!isValid) {
        System.out.println("Invalid phone number. Please try again.");
    } while (!isValid);

    System.out.println("Thank you. Your phone number has been accepted.");

Here’s how the program works:

The program starts by importing the Scanner class from the java.util package, which allows us to read input from the user.

Next, we define a main method, which is the entry point of the program.

Inside the main method, we create a Scanner object called scanner, which will be used to read the user’s input.

We also define a String variable called phoneNumber and a boolean variable called isValid.

Also Read: Java Program to Find Whether a Person is Eligible to Vote or Not

phoneNumber will be used to store the phone number that the user enters, and isValid will be used to keep track of whether the phone number is valid or not.

We start a do-while loop, which will continue to execute as long as the isValid variable is false. This means that the loop will keep running until the user enters a valid phone number.

Inside the loop, we prompt the user to enter a phone number and read their input using the scanner object.

We then use the matches method of the String class to check if the phoneNumber variable contains exactly 10 digits. The matches method uses a regular expression to check if the string matches a certain pattern. In this case, the regular expression "[0-9]{10}" will match any string that consists of 10 digits.

If the phone number is not valid (i.e., if it does not contain exactly 10 digits), we set the isValid variable to false and print an error message.

If the phone number is valid, we set the isValid variable to true and the loop will terminate.

Also Read: Sum of Odd and Even Digits in a Number in Java

Finally, we print a message thanking the user for entering a valid phone number.

Here are a few more details that you might find helpful:

The do-while loop is a control structure that allows you to repeat a block of code until a certain condition is met.

It is similar to a while loop, but the difference is that the code block inside a do-while loop is always executed at least once, even if the condition is false from the start.

This is because the condition is checked at the end of each iteration, after the code block has been executed.

In the code example above, we use the matches method of the String class to check if the phone number is a 10-digit number.

The matches method takes a regular expression as an argument and returns true if the string matches the pattern specified by the regular expression, or false if it does not.

In the regular expression "[0-9]{10}", the [0-9] part specifies that we are looking for any digit between 0 and 9. The {10} part specifies that we want exactly 10 of these digits. Together, the regular expression will match any string that consists of exactly 10 digits.

Regular expressions are a powerful tool for matching and extracting patterns in strings. They can be a bit tricky to understand at first, but they are very useful once you get the hang of them.

If you want to learn more about regular expressions, there are many resources online that can help you get started.

I hope this helps! Let me know if you have any other questions.

Leave a Comment