// Project: UnitTests // Module: phoneletters2digits // Source code file: GetDigits.java // The phoneLettersToDigits method of the GetDigits class, // convert letters to the digits of a phone number. // The GetDigitsTest class contains unit tests for // the GetDigits class method. public class GetDigits { static String phoneLettersToDigits(String number) { String[ ] letters = {"", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"}; // Look up each letter in the letters array, // the index of the letter is the corresponding // digit, which is concatenated to the output // string digits. String digits = ""; for(int i = 0; i < number.length( ); i++) { char letter = number.charAt(i); for(int j = 0; j < letters.length; j++) { if(letters[j].indexOf(letter) > -1) { digits += String.valueOf(j); break; } } } return digits; } }