Java Program to Format the String to Phone Number Format with dashes

11:58 PM 0 Comments A+ a-

Java Program to format the given input string to phone number format with dashes

Below is a coding challenge question I recently attempted in some competition. Suppose we are given a string that might contain some undesired characters apart from the digits. We are supposed to convert it into a phone number format with dashes with grouping of 3 digits. In case, only 4 digits are left in the end after grouping other numbers in a group of 3, then the numbers should be grouped in 2. In case 5 digits are left in the end, the digits should be grouped in 3 and 2. 
For example: "89437438843921" should be converted into 894-374-388-439-21
and "7836473097338" should be converted into 783-647-309-73-38
and "483974937434" should be converted into 483-974-937-434
And in case there are some alphabets and special characters, we should ignore them and remove from the output string.

Java Program to convert a given String to Phone number format with dashes:-


public class PhoneNumberFormat {
 
 public String formatToPhoneNumberFormat(String S) {
  StringBuilder filterOutJunk = new StringBuilder();
  int digitCount = 0;
  for (int i = 0;i < S.length();i++) {
   if("0123456789".indexOf(S.charAt(i))==-1)
    continue;
   digitCount++;
   filterOutJunk.append(S.charAt(i));
  }
  //if the input contains less than 2 digits, return null
  if(digitCount < 2)
   return null;
  //now the filterOutJunk contains only the clean string containing digits only
  filterOutJunk = new StringBuilder(filterOutJunk.toString());
  //start inserting the dashes after 3rd digit
  int insertDashes = 3;
  while (insertDashes < filterOutJunk.length())
  {
   //insert dashes after 3 digits
      filterOutJunk.insert(insertDashes, "-");
      //but if we are left with only four digits in the end
      //enter dashes after 2 digits (2 + 1 = 3 for the fact that dash also takes one place)
      if(insertDashes == (filterOutJunk.length() - 5))
       insertDashes = insertDashes + 3;
      else
       insertDashes = insertDashes + 4;
  }
  return filterOutJunk.toString();
 }
 
 public static void main(String[] args) {
  PhoneNumberFormat t = new PhoneNumberFormat();
  //Hard coded input for testing purpose. Take it from user.
  //Check for maximum input length of 100.
  String input = "47983"; 
  String formatted = t.formatToPhoneNumberFormat(input);
  System.out.println(formatted);
 }

}


Please try out this on your own first and then refer this in case you get stuck somewhere. This will help you learn. And in case you find out this code not working properly for any given input string, please do let me know in comments.

Currently working at Symantec in Chennai. Previously worked as a Senior Software Engineer at IGATE. Loves to explore new things, loves to code and solve challenging problems with the help of it. Like any other normal person, loves to watch movies, listen to songs (specially when travelling, it is must), getting to know people, travelling and whatever makes me feel good!