• Post Reply Bookmark Topic Watch Topic
  • New Topic

java replace correct number into letter  RSS feed

 
Defa Lukman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!