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:
- The input phone number is initialized as a string with the value “1234567890”.
- 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.
- 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.
- 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.
- The resulting formatted phone number is stored in the output variable and printed to the console using System.out.println().
- 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.
- 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:
- long phoneFmt = 123456789L;: This line initializes a long variable phoneFmt with the value 123456789L, which represents the input phone number.
- 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.
- String phoneRawString = phoneDecimalFmt.format(phoneFmt);: The input phone number phoneFmt is formatted into a string phoneRawString using the defined format from the phoneDecimalFmt object.
- 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.
- 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.
- phoneRawString.substring(0, 3): This extracts the first three digits from the phoneRawString.
- phoneRawString.substring(3, 6): This extracts the next two digits from the phoneRawString.
- phoneRawString.substring(6): This extracts the last five digits from the phoneRawString.
- 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.
- 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.
Leave a Reply