Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Telephone Number Converter

    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);
        }
    }

  • #2
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try this code.
    Code:
    private char toDigit(char letter) {
        	char numeral = '.';
    		System.out.println("letter is: "+ letter);
    		System.out.println("keyPad = "+ keyPad.length);
        	for (int i = 0; i < keyPad.length  ; i++){
    			for(int j=0;j < keyPad[i].length();j++){				
        			if (keyPad[i].charAt(j) == letter){				
        				numeral = Character.forDigit(i,10);
        				break;
        			}else
        				continue;
    			}
        	}
        	return numeral;
        }
    It is already alet and didn't spend much time in understanding why both in same loop didn't work. for code "ask" it returned me 275. With changing loops, It gave me "ttt". So I changed the logic to get the corect integer also.

  • Users who have thanked spchinta for this post:

    Jaxyral (03-10-2011)

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This worked perfectly. I knew it was a small error. Thank you for the help.

  • #4
    New Coder
    Join Date
    Mar 2011
    Location
    USA
    Posts
    23
    Thanks
    0
    Thanked 1 Time in 1 Post
    If that worked for you, Mark this resolved..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •