Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting Letters to Corresponding Numbers  RSS feed

 
jason candelora
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or...

will also work.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, you're sure right. See, I said to test very carefully!
 
jason candelora
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for all of your replys!
I will try some of this code later....
Jason
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Koontz
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh - it handles this:
n = new TelephoneNumber("(888) HOLY COW");
out.println(n.format());
prints the #s or alpha & #s
David
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!