Split
the Phone Numbers
The following is the solution to the Hacker Rank problem “Split the Phone Numbers" using Java. For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.
/**
* @author Arun.G
*
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[]
args) {
/* Enter your code here. Read input from
STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int no = sc.nextInt();
while(no-->=0)
{
String word = sc.nextLine();
int count=0;String
result="";
if(!word.equals(""))
{
//for country
String pattern ="([0-9]{1,})[
-]{1}([0-9]{1,3})[ -]{1}([0-9]{4,10})";
Pattern phonePattern =
Pattern.compile(pattern);
Matcher m =
phonePattern.matcher(word);
if(m.matches())
{
String countryCode =
m.group(1);
String localAreaCode =
m.group(2);
String Number =
m.group(3);
result ="CountryCode="+countryCode+",LocalAreaCode="+localAreaCode+",Number="+Number;
}
System.out.println(result);
}
}
}
}
|
No comments:
Post a Comment