Reputation: 43
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?
Upvotes: 0
Views: 517
Reputation: 6322
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
Upvotes: 1
Reputation: 36456
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.
Upvotes: 0