Can anyone please help me on how to convert words into numbers in java programming, using string tokenizer. Your answer will be highly appreciated. for example four hundred four should be 404 ... thanks alot
+6
A:
I'm guessing this is homework. The basic strategy would be to have a value
variable that you work with. Each time you see a string "one", "two", "eleven", "seventy" you would add that amount to value
. When you see a string like "hundred", "thousand", "million", you would multiply value
by that amount.
For larger numbers you'll probably need to create a few subtotals and combine at the end. The steps to process a number like 111,374 written out as "one hundred eleven thousand three hundred seventy four" would be
- "one" ->
value[0] += 1
(now1
) - "hundred" ->
value[0] *= 100
(now100
) - "eleven" ->
value[0] += 11
(now111
) - "thousand" ->
value[0] *= 1000
(now111000
) - "three" ->
value[1] += 3
- "hundred" ->
value[1] *= 100
(now300
) - "seventy" ->
value[1] += 70
(now370
) - "four" ->
value[1] += 4
now (374)
You'll still need to figure out how to decide when to build it as multiple values.
bemace
2010-10-31 06:20:20
You might want to put all the words into a `string szWords[]`. So, `szWords[0] = "one hundred thousand"`, `szWords[1] = "eleven hundred thousand"`, `szWords[2] = "three hundred"`, `szWords[3] = "seventy"`, `szWords[4] = "four"`
muntoo
2010-10-31 06:39:19
@muntoo - it's not clear what your suggestion is supposed to accomplish
bemace
2010-10-31 08:16:19