Many companies use telephone numbers like 555-GET-Food so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion:
- A, B, and C = 2
- D, E, and F = 3
- G, H, and I = 4
- J, K, and L = 5
- M, N, and O = 6
- P, Q, R, and S = 7
- T, U, and V = 8
- W, X, y, and Z = 9
Write an application that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD the application should display 555-438-3663.
Breaking it down
For this exercise we will use a popular third party library created by Google called guava. We could write a series of if statement with a bunch of or statements which could get kind of ugly OR find a collection that will hold a one-to-many relationship. Guava's multimap is a collection that maps keys to values, similar to Map, but in which each key may be associated with multiple values. We will use this to associate the translation between alpha and phone digit.
Create a collection to hold translated values
We will ask the user to enter input from keyboard and use guava Splitter to turn the string into a list based on a fixed length. Using java 8 syntax we will use an inline java.util.function to check if element is a character and if so look up the value in
numberAlphaTrans. Finally using java 8 we will join the elements in list.
- If a user inputs a character in for the area code, should the program ignore, throw an error or ?. Modify the program on how you feel it should work.