aspose file tools
The moose likes Beginning Java and the fly likes Converting Letters to Corresponding Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Converting Letters to Corresponding Numbers" Watch "Converting Letters to Corresponding Numbers" New topic
Author

Converting Letters to Corresponding Numbers

jason candelora
Greenhorn

Joined: Sep 25, 2003
Posts: 16
Hi,
I am workin on an excercise where I am converting phone numbers with letters and converting them to plain phone numbers with all numbers. The code I am using is very bulky and is as follows:
wordToConvert = input.replace('a', '2');
wordToConvert = wordToConvert.replace('b', '2');
wordToConvert = wordToConvert.replace('c', '2');
wordToConvert = wordToConvert.replace('d', '3');
wordToConvert = wordToConvert.replace('e', '3');
wordToConvert = wordToConvert.replace('f', '3');
wordToConvert = wordToConvert.replace('g', '4');
wordToConvert = wordToConvert.replace('h', '4');
wordToConvert = wordToConvert.replace('i', '4');
wordToConvert = wordToConvert.replace('j', '5');
wordToConvert = wordToConvert.replace('k', '5');
wordToConvert = wordToConvert.replace('l', '5');
wordToConvert = wordToConvert.replace('m', '6');
wordToConvert = wordToConvert.replace('n', '6');
wordToConvert = wordToConvert.replace('o', '6');
wordToConvert = wordToConvert.replace('p', '7');
wordToConvert = wordToConvert.replace('q', '7');
wordToConvert = wordToConvert.replace('r', '7');
wordToConvert = wordToConvert.replace('s', '7');
wordToConvert = wordToConvert.replace('t', '8');
wordToConvert = wordToConvert.replace('u', '8');
wordToConvert = wordToConvert.replace('v', '8');
wordToConvert = wordToConvert.replace('w', '9');
wordToConvert = wordToConvert.replace('x', '9');
wordToConvert = wordToConvert.replace('y', '9');
wordToConvert = wordToConvert.replace('z', '9');
wordToConvert = wordToConvert.replace('A', '2');
wordToConvert = wordToConvert.replace('B', '2');
wordToConvert = wordToConvert.replace('C', '2');
wordToConvert = wordToConvert.replace('D', '3');
wordToConvert = wordToConvert.replace('E', '3');
wordToConvert = wordToConvert.replace('F', '3');
wordToConvert = wordToConvert.replace('G', '4');
wordToConvert = wordToConvert.replace('H', '4');
wordToConvert = wordToConvert.replace('I', '4');
wordToConvert = wordToConvert.replace('J', '5');
wordToConvert = wordToConvert.replace('K', '5');
wordToConvert = wordToConvert.replace('L', '5');
wordToConvert = wordToConvert.replace('M', '6');
wordToConvert = wordToConvert.replace('N', '6');
wordToConvert = wordToConvert.replace('O', '6');
wordToConvert = wordToConvert.replace('P', '7');
wordToConvert = wordToConvert.replace('Q', '7');
wordToConvert = wordToConvert.replace('R', '7');
wordToConvert = wordToConvert.replace('S', '7');
wordToConvert = wordToConvert.replace('T', '8');
wordToConvert = wordToConvert.replace('U', '8');
wordToConvert = wordToConvert.replace('V', '8');
wordToConvert = wordToConvert.replace('W', '9');
wordToConvert = wordToConvert.replace('X', '9');
wordToConvert = wordToConvert.replace('Y', '9');
wordToConvert = wordToConvert.replace('Z', '9');

Is there a more compact way to do this??
Thanks for any help!
Jason
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Yes, that is bulky. What you might want to do is set up a Hash to handle this for you. Also, you can use regualr expressions to slim down the code a bit.

This print out 666-4242.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24040
    
  13

Hi Jason,
Welcome to JavaRanch!
Wow. Yeah, that's not the most efficient way to do things, because each "replace" is allocating a whole new string and copying all the data over.
I'd do this using a lookup table, since the translation is slightly irregular, something like

I'd test this code thoroughly before using it, as I just made it up off the top of my head. Note I used the explicit less-than/greater-than tests instead of Character.isLetter() so that non-ASCII letters simply wouldn't be converted; you might want to detect these and throw an exception of something.


[Jess in Action][AskingGoodQuestions]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Or...

will also work.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Originally posted by Ernest Friedman-Hill:


Don't forget that you'd need single quotes around all of those digits: '2', '2', '2', etc. Otherwise you'd get some strangeness as the output.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24040
    
  13

Yep, you're sure right. See, I said to test very carefully!
jason candelora
Greenhorn

Joined: Sep 25, 2003
Posts: 16
Thanks very much for all of your replys!
I will try some of this code later....
Jason
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Ernest Friedman-Hill:
Wow. Yeah, that's not the most efficient way to do things, because each "replace" is allocating a whole new string and copying all the data over.
Thankfully, it isn't quite as bad as that! If nothing is replaced then the original String is returned and no new String is created. So at worst we are creating six extra Strings that we didn't need.
You will get no argument from me as to the inefficiency of running all those replace methods though!
Another way to do it would be to convert the String into a char array and then use a switch statement to change the characters. No specific advantage to this. Just one more way to do the same thing.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
David Koontz
Greenhorn

Joined: Oct 08, 2003
Posts: 14
Perhaps my solution is not as effecient, however...
Just submitted a class TelephoneNumber to do phone formating to
SourceForge
http://sourceforge.net/snippet/detail.php?type=snippet&id=101225
The TelephoneNumber class represents a US telephone number.
This class contains many constructors for various uses. It then stores the telephone number internally in seperate codes (area code, exchange, etc.). This encourages
use of the format method to output the telephone number in any style the programmer wishes.
[needs i18n work]
Example usage:
long number = 8005551212L;
TelephoneNumber phone = new TelephoneNumber(number);
StringBuffer msg = new StringBuffer("Call me at ");
msg.append(phone.format());
// msg is now "Call me at (800) 555-1212"
David Koontz
Greenhorn

Joined: Oct 08, 2003
Posts: 14
Oh - it handles this:
n = new TelephoneNumber("(888) HOLY COW");
out.println(n.format());
prints the #s or alpha & #s
David
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Converting Letters to Corresponding Numbers
 
Similar Threads
extracting image binary data from file
Google Select
Stripes 2.5.1 - Is colon allowed in the URLBinding to map variables?
extracting image binary data from file
Captcha problem