Reputation: 51
I am using libphonenumber jar (version 7) and trying to parse phone number without setting Region. I saw following code as recommended.
PhoneNumber phoneNumber = phoneUtil.parse(numberStr, ""));
However, this is throwing INVALID_COUNTRY_CODE exception. Did something change in the latest version? How can I get region code (US) for given number like ("+1-xxx-xxx-xxxx")?
Upvotes: 1
Views: 6738
Reputation: 31
I used libphonenumber 7.0.8 and it worked:
public static void main(final String[] args) {
String numberStr = "+1 650-253-0000"; // Google
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
try {
PhoneNumber phoneNumber = phoneUtil.parse(numberStr, null);
System.out.println(geocoder.getDescriptionForNumber(phoneNumber, Locale.ENGLISH));
} catch (NumberParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It worked using:
phoneUtil.parse(numberStr, null);
phoneUtil.parse(numberStr, "");
phoneUtil.parse(numberStr, "ZZ");
Output is always Mountain View, CA
- so the number was parsed successfully.
From the javadoc you can find this information:
If the number is guaranteed to start with a '+' followed by the country calling code, then "ZZ" or null can be supplied.
Upvotes: 2