Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-10-2011, 05:02 AM   PM User | #1
Jaxyral
New Coder

 
Join Date: Apr 2010
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jaxyral is an unknown quantity at this point
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);
    }
}
Jaxyral is offline   Reply With Quote
Old 03-10-2011, 07:22 AM   PM User | #2
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
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.
spchinta is offline   Reply With Quote
Users who have thanked spchinta for this post:
Jaxyral (03-10-2011)
Old 03-10-2011, 09:29 AM   PM User | #3
Jaxyral
New Coder

 
Join Date: Apr 2010
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jaxyral is an unknown quantity at this point
This worked perfectly. I knew it was a small error. Thank you for the help.
Jaxyral is offline   Reply With Quote
Old 03-10-2011, 05:37 PM   PM User | #4
spchinta
New Coder

 
Join Date: Mar 2011
Location: USA
Posts: 23
Thanks: 0
Thanked 1 Time in 1 Post
spchinta is an unknown quantity at this point
If that worked for you, Mark this resolved..
spchinta is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:11 PM.


Advertisement
Log in to turn off these ads.