0

Is there any way to find the country code of a phone number in Java? Say if I give 9710334544, I will receive the country code as 91 (if its India).

Any suggestions please.

  • 10
    No, there's no way you can extract 91 from a number that doesn't even contain it... – David M Jul 02 '12 at 13:12
  • 2
    Good old division should do the trick... – NominSim Jul 02 '12 at 13:14
  • 3
    Suggestions? get a list of country calling codes and look up the prefix? – Alex K. Jul 02 '12 at 13:15
  • @NominSim - was just wondering if this was some modulo-related trickery... :) – David M Jul 02 '12 at 13:15
  • 2
    This might be useful. http://stackoverflow.com/questions/487906/java-phone-number-format-api – Colin D Jul 02 '12 at 13:18
  • 2
    Umh if its india? what happens if there is another number in the world thats the same, how would you know the country code or specifically which country a given number is from? you'd atleast need the location of the input i.e the user input was from India, thus you now just need the countries prefixes, google can help with that – David Kroukamp Jul 02 '12 at 13:20

2 Answers2

5

If the phone number does not include the country code number as prefix, there is no way to find out from which region this phone number originates.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • converting to a string makes me question this solution. but +1 for mention the libphonenumber library. – Colin D Jul 02 '12 at 13:21
  • either way the OP would still need to know the region/country,which he doesnt say is specified, hence he wants to know if its possible to get a country code, from a number that diesn not have its country code/region prepended – David Kroukamp Jul 02 '12 at 13:23
  • 1
    @DavidKroukamp Agreed. I first through the OP had a typo in his question, but looking at the number of digits it should be clear that this information is not available. Hence, I've edited my answer. – Konrad Reiche Jul 02 '12 at 13:29
  • @platzhirsch good change. +1 dont know how the OP would expect to find the country code from a random sequence of numbers – David Kroukamp Jul 02 '12 at 13:31
2

The idea behind the country code is to distinguish the country first, and then parse the number. The reason for this is to forego issues with the same number.

If my U.S. number is 1234567890 then what is there to distinguish that from my U.K number which is 1234567890? The answer is the country prefix. Unfortunately, due to the very nature of this number(in that it distinguishes between numbers that are the same, you can't use the number to figure it out).

Now, if you already have the full 13-14 digit number, you can find the country code by simply dividing (integer division):

long inputPhoneNumber = 123 (XXX) - XXX - XXXX;
long countryCode = inputPhoneNumber / 10000000000l;
// Will give 123

After you have the answer you can match it up with the country, the internet provides several sites that list the codes with their countries:

Country Code

NominSim
  • 8,447
  • 3
  • 28
  • 38