Question

I get a problem about returning phone keypads. For instance, if I input 1-80-0Apple, it will return 1-80-027753. Could somebody tell me how to figure out this one?

Was it helpful?

Solution

If you are going to deal with different formats for phone numbers entered by users I would suggest that you use Google's libphonenumber (http://code.google.com/p/libphonenumber/). It has lots of utilities for handling phone numbers.

For the case that you ask, you can do this:

String phoneNumber = PhoneNumberUtil.convertAlphaCharactersInNumber("1-80-0Apple");
System.out.println(phoneNumber);

That would print:

1-80-027753

OTHER TIPS

Maintain a lookup table between letters and numbers:

A -> 2
B -> 2
C -> 2
D -> 3
etc

Then loop through each letter in the number and replace it with the corresponding number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top