Java program that turns a string containing a 10 digit phone number into a more readable string with parentheses and dashes
Posted by Samath
Last Updated: February 11, 2021

Write a java program that turns a string containing a 10 digit phone number into a more readable string with parentheses and dashes.

Code:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String tel_number;
        String area_code;
        String next_three;
        String last_four;
        
        System.out.print("Enter 10 digit number: ");
        tel_number = input.next();
        input.close();
        
        area_code = tel_number.substring(0, 3);
        next_three = tel_number.substring(3, 6);
        last_four = tel_number.substring(6, 10);
        System.out.printf("(%s) %s-%s", area_code, next_three, last_four);
    }
}