Question:
Converting Words Into Numbers in Java?
anonymous
2009-02-16 10:37:54 UTC
I have an assignment that requires me to convert words of numbers into actual integer numbers. i.e. Two thousand three hundred and seventeen would output 2317. I know how to do it when it's going from 2317 -> words, but I'm having a hard time figuring how to do the opposite. I'm not asking for the code, but some help on how to approach this problem.

Any help is appreciated, thanks.
Five answers:
?
2009-02-16 11:17:28 UTC
This is an AWESOME assignment. One that I would enjoy doing myself. Okay, how to approach it.



Well, overall you are doing two things: Finding tokens (words that translates to numbers) and applying grammar. In short, you are building a parser for a very limited language.



The tokens you would need are:



POWER: thousand, million, billion

HUNDRED: hundred

TEN: twenty, thirty... ninety

UNIT: one, two, three, ... nine,

SPECIAL: ten, eleven, twelve, ... nineteen



(drop any "and"s as they are meaningless. Break hyphens into two tokens. That is sixty-five should be processed as "sixty" "five")



Once you've tokenized your string, move from RIGHT TO LEFT.



1. Grab all the tokens from the RIGHT until you hit a POWER or the whole string.



2. Parse the tokens after the stop point for these patterns:

SPECIAL

TEN

UNIT

TEN UNIT

UNIT HUNDRED

UNIT HUNDRED SPECIAL

UNIT HUNDRED TEN

UNIT HUNDRED UNIT

UNIT HUNDRED TEN UNIT



(This assumes that "seventeen hundred" is not allowed in this grammar)



This gives you the last three digits of your number.



3. If you stopped at the whole string you are done.



4. If you stopped at a power, start again at step 1 until you reach a higher POWER or the whole string.
may
2016-11-06 13:33:45 UTC
Converting Words To Numbers
Atif H
2009-02-16 10:49:02 UTC
well im not really sure about it, and dont even know if theres a function or anything for this although there probably is. but i think wat can be done, a much longer process though, read out and compare the string. and then have it replaced with the respective numbers and add them up. like, two thousand three hundred seventeen

2 X 1000 + 3 X 100 + 1 X 10 + 7





and if its number strings to int.. as in.. string "2317" to int 2317 ... thats gonna be >

variableName = Integer.Parseint(variableName);





hope this helped in someway or the other. but im pretty sure they have a function or method for it.
By the Way...
2009-02-16 10:47:11 UTC
wtf.



What level of programming is this class?



And are you sure that he doesn't mean "number strings" to integers as in convert "2317" (a string) to 2317 (an int)?



... this is the weirdest assignment I have ever seen.



Edit: M L gave a great answer!
Lemon Juice
2014-01-28 05:56:00 UTC
conver words and phrases number? 7237, please how show me.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...