File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Detecting country by Phone Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Detecting country by Phone Number" Watch "Detecting country by Phone Number" New topic
Author

Detecting country by Phone Number

Houssem Jlassi
Greenhorn

Joined: Jul 07, 2012
Posts: 7
Hello EveryBody,
I need to make a UI that calculate the fees of communication for my entreprise.
In the DataBase I have :
*a Table that contain (Country Code : Fee per Minute)
*a Table that contain(Phone number : Duration)

I need to calculate the total fee, the problem is about detecting the country in the phone number(from worldwide), I have no standardformat for the phone number.

I need your HELP GreenHorns.
Thanks & Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

I will give you one example why this will never work 100% correctly. But first an introduction.

A phone number in most countries is built from three parts: the international calling code, the area code, and the subscription number. For example, +1-555-1234 has an international calling code of +1, an area code of 555 and a subscription number of 1234. You would think that using the international calling code would help you out finding the country. That's where my example comes in.

The US has international calling code +1. That makes you think that anything starting with +1 would come from the US. That's where you would think wrongly:
American Samoa uses +1-684.
Anguilla uses +1-264.
Antigua and Barbuda uses +1-268.
Bahamas uses +1-242.
Barbados uses +1-246.
Bermuda uses +1-441.
Cayman Islands uses +1-345.
Dominica uses +1-767.
Dominican Republic uses +1-809.
Grenada uses +1-473.
Guam uses +1-671.
Jamaica uses +1-876.
Montserrat uses +1-664.
Northern Mariana Islands uses +1-670.
Puerto Rico uses +1-787.
Saint Kitts and Nevis uses +1-869.
Saint Lucia uses +1-758.
Saint Vincent and the Grenadines uses +1-784.
Trinidad and Tobago uses +1-868.
Turks and Caicos Islands uses +1-649.
Virgin Islands, U.S. uses +1-340.

So now you're thinking, "but I can check for those, and there wouldn't be a problem." Unfortunately, you'd be wrong again:
United States uses +1.
United States Minor Outlying Islands uses +1.
Canada uses +1.

And there you have it - one international calling code used by three different countries.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Rob Spoor wrote:So now you're thinking, "but I can check for those, and there wouldn't be a problem." Unfortunately, you'd be wrong again:
United States Minor Outlying Islands uses +1.

Actually, according to this page, it uses +1-699.

Canada uses +1.

And this page lists the area codes used by Canada.

So you could theoretically create a list for those exceptions, (and those darn exchange ones, like 555) and then everything else is "the US". But I agree, it's not ideal.

@Houssem: Fortunately, most other countries don't have the same issue; although if this is a tariff system, you may well have problems with somewhere like Russia, because its quite possible that phoning Moscow from Vladivostok is actually more expensive than phoning Tokyo or Seoul, which are a lot closer.

I suspect you have quite a lot of prep work ahead of you.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Houssem Jlassi
Greenhorn

Joined: Jul 07, 2012
Posts: 7
Thanks for the answers
I found this on the net : http://senderbox.net/detect_country_by_phone_number.html
This is what I need, and then when I find the country it'll be easier...
Does anyone have an idea about how this senderbox works ?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

It uses an XML document to describe the countries. This splits the countries first per first digit which apparently can be used to distinguish the region (1 == North America, 3 and 4 == Europe, etc), then the next digit(s) to find the country. The UK for instance is +44, so is found in the region with code 4, then 4 underneath that for the UK itself. Unfortunately for you, that list fails miserably for the US and Canada.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Detecting country by Phone Number