I'm looking for a package that can convert a phone number to its country code

i know how to write it myself
but beter to check the net first

anybody ever heard of something like this?

eg:
0032586987 > Belgium

please give me a link
it would save me enormous amounts of work

Don't know the answer to your question. But won't it be fun to write it? :)
I think a tree would be quite suitable.

Don't know the answer to your question. But won't it be fun to write it? :)
I think a tree would be quite suitable.

i got the info already
do you know there are around 195 countries

i was thinking of a if else
if( number.substring(1,5) .equals(0032)){
return "BE"
}

got a better way of dealing with this?
the substring isn't always the same, sometimes its 5 numbers that have to match, sometimes 4 or 3

and the total length is also a factor

this is going to be super boring to do :(
but i'll do it :)

As I said, tree is the best thing I can think of.
Somethihg like this (See attached):
- One digit per node.
- Only a leaf node corresponds to a match. So contains the country name.
While matching you just have to keep going (matching digit by digit) until you reach a leaf node (node without children), just pick up the country name from it and return.

This should be most performant unless Narue says otherwise, think she's the guru of data structures around here.. :)

As I said, tree is the best thing I can think of.
Somethihg like this (See attached):
- One digit per node.
- Only a leaf node corresponds to a match. So contains the country name.
While matching you just have to keep going (matching digit by digit) until you reach a leaf node (node without children), just pick up the country name from it and return.

This should be most performant unless Narue says otherwise, think she's the guru of data structures around here.. :)

hmm
ok
but then i can't use the total length of the number?
except if i take different trees for every number length?

i'll send pm to Narue to check this topic :)

>This should be most performant unless Narue says otherwise
AFAIK, country codes are organized in a prefix hierarchy, so a trie would be the most direct data structure for the hierarchy. Obviously reusing an existing library is ideal, but because there aren't many country codes, you could use just about anything and it'll be fast enough.

However, unless there's a required format for the input string where the country code is separate, some kind of prefix data structure (like a trie) where the query can be refined at each subsequent prefix would make the job much easier.

Edited 6 Years Ago by Narue: n/a

>> but then i can't use the total length of the number?
>> except if i take different trees for every number length?
Not sure what you mean..
You can bind all "branches" to a dummy root node if you like (which actually could be "0").
Why do you want/need to use the full length of the number? Once you reach the leaf node, you're assured of finding the correct country name.

PS: I checked the codes, I saw that it's guaranteed to have NO overlaps. I.e. you can't have 2 codes, where code1 is a substring of code2.

Edited 6 Years Ago by thekashyap: n/a

>> but then i can't use the total length of the number?
>> except if i take different trees for every number length?
Not sure what you mean..
You can bind all "branches" to a dummy root node if you like (which actually could be "0").
Why do you want/need to use the full length of the number? Once you reach the leaf node, you're assured of finding the correct country name.

PS: I checked the codes, I saw that it's guaranteed to have NO overlaps. I.e. you can't have 2 codes, where code1 is a substring of code2.

well, i need to be able to detect mobile and local numbers
but your right
i can check for the country prefix
and when i found what country it is, then check if its cellphone or normal phone number

thx for your help :)
i'll program the tree begin february
and make the package public avalible
I'll let you know how it turns out

grtz

Bert

Put the code in the code snippet section.. Next time I can just give a link to it.. :)
Mark the thread solved if there is nothing further..

i finished the implementation
i used a database table (needed a database anyway)

here is the file with regexps
http://berty.110mb.com/files/telephone_number_analyse.xls

this is the class that does the analyses:

import java.sql.SQLException;
import java.util.ArrayList;
import main.DataConnector;

public class TelephoneAnalyser {

    private static TelephoneAnalyser instance;
    private static ArrayList<ArrayList<RegexpCountry>> countries;
    private static RegexpCountry belgium;

    public static TelephoneAnalyser getInstance() {
    if (instance == null) {
        instance = new TelephoneAnalyser();
    }
    return instance;
    }

    public TelephoneAnalyser() {
    try {
        countries = DataConnector.getRegexpCountries();
        belgium = DataConnector.getRegexpBelgium();
    } catch (SQLException ex) {
        System.out.println("Error, problem with getting regexps for cost analysis");
    }
    }

    //number is input and is not changed en country and price are output parameters
    public boolean analyse(String number, String countryCode, int price) {
    System.out.println("start analyse");
    System.out.println("number: " + number);
    String num = number;
    num.replaceFirst("^0*", "");
    System.out.println("number: " + num);
    int start = Integer.parseInt(num.substring(1, 2));
    for (RegexpCountry country : countries.get(start - 1)) {
        if (num.matches(country.getRegexpCountry())) {
        countryCode = country.getCode();
        if (num.matches(country.getRegexpMobile())) {
            price = country.getPriceMobile();
        } else {
            price = country.getPriceFix();
        }
        return true;
        }
    }
    return false;
    }
}

countryregexp:

public class RegexpCountry {

    private String code;
    private String labelEn;
    private String labelNl;
    private String regexpCountry;
    private String regexpMobile;
    private int priceFix;
    private int priceMobile;


    public RegexpCountry(String labelNl, String code) {
        this.code = code;
        this.labelNl = labelNl;
    }

    public RegexpCountry(String labelEn, String labelNl, String code, String regexpCountry, String regexpMobile, int priceFix, int priceMobile) {
        this.code = code;
        this.labelEn = labelEn;
        this.labelNl = labelNl;
        this.regexpCountry = regexpCountry;
        this.regexpMobile = regexpMobile;
        this.priceFix = priceFix;
        this.priceMobile = priceMobile;
    }

    public String getCode() {
        return code;
    }

    public String getLabelEn() {
        return labelEn;
    }

    public String getLabelNl() {
        return labelNl;
    }

    public int getPriceFix() {
        return priceFix;
    }

    public int getPriceMobile() {
        return priceMobile;
    }

    public String getRegexpCountry() {
        return regexpCountry;
    }

    public String getRegexpMobile() {
        return regexpMobile;
    }

    @Override
    public String toString(){
        return labelNl;
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

There are a number of free sites on-line which can be used. Keep in mind sometimes there is no information provided due to the service being free and relying solely on volunteered information, which is obtained from a public database. One of the most extensive free sites is Yellowpages.com; it’s a good website available 24/7, which you can use to do a Reverse Cell Phone Number Search.

thx

that might have been a good solution
but the current solution works fine for me

thx anyways :)

This question has already been answered. Start a new discussion instead.