PhoneNumberLibrary { public static String[] segmentPhoneNumber(String number) { number = normalizePhoneNumber(number); String callingCode = null; String areaCode = null; String subscriberNumberA = null; String subscriberNumberB = number; if (subscriberNumberB != null) { int n = number.length(); if (n > 4) { subscriberNumberA = subscriberNumberB.substring(0, n - 4); subscriberNumberB = subscriberNumberB.substring(n - 4); } } if (subscriberNumberA != null) { int n = subscriberNumberA.length(); if (n > 3) { areaCode = subscriberNumberA.substring(0, n - 3); subscriberNumberA = subscriberNumberA.substring(n - 3); } } if (areaCode != null) { int n = areaCode.length(); if (n > 3) { callingCode = areaCode.substring(0, n - 3); areaCode = areaCode.substring(n - 3); } } List<String> tokens = new ArrayList<String>(); if (callingCode != null) tokens.add(callingCode); if (areaCode != null) tokens.add(areaCode); if (subscriberNumberA != null) tokens.add(subscriberNumberA); if (subscriberNumberB != null) tokens.add(subscriberNumberB); return tokens.toArray(new String[tokens.size()]); }  static String normalizePhoneNumber(String phoneNumber); static String[] segmentPhoneNumber(String number);  }
@Test public void testSegmentPhoneNumber() { String[] segments = PhoneNumberLibrary.segmentPhoneNumber("12063038220"); assertEquals(4,segments.length); assertEquals("1",segments[0]); assertEquals("206",segments[1]); assertEquals("303",segments[2]); assertEquals("8220",segments[3]); segments = PhoneNumberLibrary.segmentPhoneNumber("2063038220"); assertEquals(4,segments.length); assertEquals("1",segments[0]); assertEquals("206",segments[1]); assertEquals("303",segments[2]); assertEquals("8220",segments[3]); segments = PhoneNumberLibrary.segmentPhoneNumber("3038220"); assertEquals(2,segments.length); assertEquals("303",segments[0]); assertEquals("8220",segments[1]); segments = PhoneNumberLibrary.segmentPhoneNumber("038220"); assertEquals(2,segments.length); assertEquals("03",segments[0]); assertEquals("8220",segments[1]); segments = PhoneNumberLibrary.segmentPhoneNumber("8220"); assertEquals(1,segments.length); assertEquals("8220",segments[0]); }