# java replace correct number into letter

• March 8th, 2014, 10:12 AM
speaky
java replace correct number into letter
Well, this is my first time get here.

I'm trying to figure out the correct way to replace number into letter. In this case, I need two steps.

First, convert letter to number. Second, restore number to word.

Words list: a = 1, b = 2, f = 6 and k = 11.

I have word: "baafk"

So, for first step, it must be: "211611"

Number "211611" must be converted to "baafk".

But, I failed at second step.

Code I've tried:
Code :

```public class str_number { public static void main(String[] args){ String word = "baafk"; String number = word.replace("a", "1").replace("b","2").replace("f","6").replace("k","11"); System.out.println(word); System.out.println(number);   System.out.println();   String text = number.replace("11", "k").replace("6","f").replace("2","b").replace("1","a"); System.out.println(number); System.out.println(text); } }```
Result for converting to number: baafk = 211611

But, result for converting above number to letter: 211611 = bkfk

What do I miss here?

How to distinguish if 11 is for "aa" and for "k"? Do you have any solutions or other ways for this case?

Thank you.
• March 8th, 2014, 10:48 AM
jashburn
Re: java replace correct number into letter
The most direct way to address this would be to add a delimiter between each number so that you can tell the difference between "11" for a "k", and "1,1" (using a comma delimiter) for two "a"s.

What is the background behind your question? I.e., why do you need to do this conversion?
• March 8th, 2014, 10:48 AM
Norm
Re: java replace correct number into letter
Quote:

How to distinguish if 11 is for "aa" and for "k"?
I don't know that there is a way. The numbers need delimiters/boundaries so you can tell where they begin and end.
• March 8th, 2014, 05:39 PM
jashburn
Re: java replace correct number into letter
Another way would be to use 2 digits to represent each letter, e.g., a = 01, b = 02, etc. This way you don't need to use delimiters/boundaries.