• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP to Format a Phone Number in Human-Readable View

WAP to Format a Phone Number in Human-Readable View

Last Updated on: July 25, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Format a Phone Number in Human-Readable View

The program InterviewPrograms50 aims to format a given phone number by inserting parentheses and hyphens at specific positions to make it more readable.

Format a Phone Number in Human-Readable View

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms50 
{
	/*
	 * Input is a phone number Ex : 1234567890 
	 * Output: (123)45-67-890
	 */
	public static void main(String[] args) 
	{
		String input="1234567890";
		
		String output=input.replaceFirst("(\\d{3})(\\d{2})(\\d{2})(\\d+)", "($1)$2-$3-$4");
		System.out.println(output);
		
		String output1="("+input.substring(0,3)+")"+input.substring(3,5)+"-"+input.substring(5,7)+"-"+input.substring(7);
		System.out.println(output1);
	}
}

Output

(123)45-67-890
(123)45-67-890

Here’s how the program works:

  1. The input phone number is initialized as a string with the value “1234567890”.
  2. The program uses the replaceFirst() method to apply a regular expression pattern on the input phone number. The regular expression (\\d{3})(\\d{2})(\\d{2})(\\d+) is used to capture groups of digits with specific lengths.
  3. The regular expression contains four capturing groups represented by (\\d{3}), (\\d{2}), (\\d{2}), and (\\d+), which correspond to the area code, first two digits, next two digits, and the remaining digits of the phone number, respectively.
  4. The replacement pattern ($1)$2-$3-$4 is used to format the phone number. The $1, $2, $3, and $4 represent the captured groups, which will be substituted with their respective values from the regular expression.
  5. The resulting formatted phone number is stored in the output variable and printed to the console using System.out.println().
  6. Additionally, the program uses string slicing with substring() method to achieve the same formatting without using regular expressions. The output1 variable stores the formatted phone number using this approach.
  7. The program then prints both formatted phone numbers to the console.

In this specific case, the program will output:

(123)45-67-890
(123)45-67-890

Both outputs represent the same phone number with the area code enclosed in parentheses, followed by two-digit groupings separated by hyphens. The final group may have more than two digits if the input phone number has more than 10 digits.

Alternative Way 1:

This Java program takes an input phone number and formats it into a specific pattern. The input is a 10-digit long phone number (represented as phoneFmt), and the desired output is in the format (XXX)-XX-XX-XXX, where X represents a digit from the original number.

package com.softwaretestingo.interviewprograms;
import java.text.DecimalFormat;
public class InterviewPrograms50_1 
{
	/*
	 * Input is a phone number Ex : 1234567890 
	 * Output: (123)45-67-890
	 */
	public static void main(String[] args) 
	{
		long phoneFmt = 123456789L;
		//get a 12 digits String, filling with left '0' (on the prefix)   
		DecimalFormat phoneDecimalFmt = new DecimalFormat("0000000000");
		String phoneRawString= phoneDecimalFmt.format(phoneFmt);

		java.text.MessageFormat phoneMsgFmt=new java.text.MessageFormat("({0})-{1}-{2}");
		//suposing a grouping of 3-3-4
		String[] phoneNumArr={phoneRawString.substring(0, 3),
				phoneRawString.substring(3,6),
				phoneRawString.substring(6)};

		System.out.println(phoneMsgFmt.format(phoneNumArr));
	}
}

Output:

(012)-345-6789

Let’s go through the program step-by-step:

  1. long phoneFmt = 123456789L;: This line initializes a long variable phoneFmt with the value 123456789L, which represents the input phone number.
  2. DecimalFormat phoneDecimalFmt = new DecimalFormat(“0000000000”);: Here, we create a DecimalFormat object called phoneDecimalFmt. The format pattern “0000000000” ensures that the output will always be a 10-digit string, filled with leading zeros if necessary.
  3. String phoneRawString = phoneDecimalFmt.format(phoneFmt);: The input phone number phoneFmt is formatted into a string phoneRawString using the defined format from the phoneDecimalFmt object.
  4. java.text.MessageFormat phoneMsgFmt = new java.text.MessageFormat(“({0})-{1}-{2}”);: A MessageFormat object called phoneMsgFmt is created with the pattern “({0})-{1}-{2}”. This pattern will be used to format the phone number into the desired output pattern.
  5. String[] phoneNumArr = {…};: An array phoneNumArr of strings is declared to hold the formatted phone number digits. The phone number is divided into three groups: the first three digits, the next two digits, and the last five digits.
  6. phoneRawString.substring(0, 3): This extracts the first three digits from the phoneRawString.
  7. phoneRawString.substring(3, 6): This extracts the next two digits from the phoneRawString.
  8. phoneRawString.substring(6): This extracts the last five digits from the phoneRawString.
  9. phoneMsgFmt.format(phoneNumArr): The format() method of phoneMsgFmt is used to format the phoneNumArr array according to the pattern “({0})-{1}-{2}”. The placeholders {0}, {1}, and {2} will be replaced with the elements from the phoneNumArr array.
  10. System.out.println(…): The formatted phone number is printed to the console, resulting in the output format (XXX)-XX-XX-XXX.

In summary, this program demonstrates how to format a 10-digit phone number into a specific pattern (XXX)-XX-XX-XXX using DecimalFormat and MessageFormat classes in Java.

    WAP to Move Digits to Left and Characters to Right
    WAP to Move Digits to Left and Characters to Right
    WAP to Print All Possible Number Combinations Whose Sum is 10
    WAP to Print All Possible Number Combinations Whose Sum is...
    WAP to Count Occurrence of Characters in a String
    WAP to Count Occurrence of Characters in a String
    Palindrome Number In Java Example
    Palindrome Number In Java Example Program
    WAP to Separate Two Characters With Digit
    WAP to Separate Two Characters With a Digit
    WAP to Reverse String Without Affecting Digits Position
    WAP to Reverse String Without Affecting Digits Position
    WAP to Print Character and Occurrences Count
    WAP to Print Character and Occurrences Count
    WAP to Remove Digits Only From a String
    WAP to Remove Digits Only From a String
    Real Time Java Program Interview Questions
    Top 70 Java Program Interview Questions
    Print Character And Count Of a Word
    Write a Java Program[ abbcccdeee –>a1b2c3d1e3]

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers