programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# java replace correct number into letter

Defa Lukman
Greenhorn
Posts: 4
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:

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.

Joel Christophel
Ranch Hand
Posts: 250
1
Welcome to the ranch!

Your "11" problem can be solved by adding commas (or any other delimiter) to the String in between separate numbers. You could also use an array of bytes to hold your values

Winston Gutkowski
Bartender
Posts: 10575
66
Speaky Share wrote:So, for first step, it must be: "211611"
Number "211611" must be converted to "baafk".
But, I failed at second step.

Unsurprising, because there is nothing in the string "211611" that allows your program to know that the first "11" is two "a"s and the second "11" is actually a "k".

As Joel said, delimiters are one way to go. Otherwise, you need to come up with a logical explanation (ie, a set of rules) for why the conversion should be done that particular way.

BTW: Welcome to JavaRanch.

Winston

Campbell Ritchie
Marshal
Posts: 56570
172
Welcome again
Did you know that chars are not characters or letters. They are numbers, and you can therefore do arithmetic on them.

As an alternative, find out about the %d tag; you can print 01 for a and then use two digits for every character. Just don't try writing 08 for h in the code.