I am writing a program to convert telephone numbers with letters in them to a telephone of all numbers using the same scheme on a telephone keypad. I have everything working except the private char toDigit method. Any help would be wonderful. I do not have much time, and would rather not stare at this all night which i would probably eventually get it considering it is probably something I am over complicating again. My main problem is traversing the arrays properly.
Code:
package extraCredit;
// CodeToPhone: map letters and digits on a phone keypad
import java.util.Scanner;
public class CodeToPhone {
// Use an array as a table of the mapping between
// letters and the digit of the key on which they appear.
// Such an array should be "static final".
private static final String keyPad[] = {"aaa","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"};
// The digits making up the phone number
private String phoneNumber;
/** Constructor takes a String consisting of letters and digits */
public CodeToPhone( String s ) {
this.phoneNumber = toDigit(justDigitsAndLetters(s));
}
/** Produce the phone number as a String */
public String toString() {
String st = new String(this.phoneNumber);
int position = 0;
// between 4-7
if (this.phoneNumber.length() <= 7 && this.phoneNumber.length() > 4 ){
//find position after last four
position = st.length() - 4;
st = new StringBuffer(st).insert(position, "-").toString();
// between 0-3
}else if (this.phoneNumber.length() <= 4){
return st;
// between 8-9
}else if (this.phoneNumber.length() < 10 && this.phoneNumber.length() > 7){
int removeNum;
//find the number of digits to remove
if(st.length() == 8){
removeNum = 1;
}else
removeNum = 2;
st = st.substring(0, st.length() - removeNum);
// treat as a 7 digit string
position = st.length() - 4;
st = new StringBuffer(st).insert(position, "-").toString();
// 10 and 11
}else if (this.phoneNumber.length() == 11 || this.phoneNumber.length() == 10){
if (this.phoneNumber.length() == 10){
st = new StringBuffer(st).insert(0, "1").toString();
}
position = st.length() - 4;
st = new StringBuffer(st).insert(position, "-").toString();
position = st.length() - 8;
st = new StringBuffer(st).insert(position, "-").toString();
position = st.length() - 12;
st = new StringBuffer(st).insert(position, "-").toString();
// over 11
}else if (this.phoneNumber.length() > 11){
int removeNum;
removeNum = st.length() - 10;
st = st.substring(0, st.length() - removeNum);
st = new StringBuffer(st).insert(0, "1").toString();
position = st.length() - 4;
st = new StringBuffer(st).insert(position, "-").toString();
position = st.length() - 8;
st = new StringBuffer(st).insert(position, "-").toString();
position = st.length() - 12;
st = new StringBuffer(st).insert(position, "-").toString();
}
return st;
}
// Delete anything that is not a letter or digit
private String justDigitsAndLetters(String s) {
String newString = new String(s);
newString = newString.replaceAll("\\W", "");
newString = newString.toUpperCase();
return newString;
}
// Map letters in a String to their corresponding digit
private String toDigit(String s) {
String newString = new String("");
for (int i = 0; i < s.length(); i++){
if(Character.isLetter(s.charAt(i))){
newString += toDigit(s.charAt(i));
}else
newString += s.charAt(i);
}
return newString;
}
// Map a letter to its corresponding digit
private char toDigit(char letter) {
char numeral = '.';
for (int i = 0, j = 0; i < keyPad.length && j < keyPad[i].length() ; i++, j++){
if (keyPad[i].charAt(j) == letter){
numeral = 't';
break;
}else
continue;
}
return numeral;
}
// Main method to test the class
public static void main(String[] args) {
// Prompt for number if not on command line
String phoneword;
if (args.length < 1) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the phone number word: ");
phoneword = input.nextLine();
} else {
phoneword = args[0];
}
CodeToPhone phone = new CodeToPhone(phoneword);
System.out.println(phone);
}
}