* Twenty-Three Quadrillion, Three Hundred and Seventy-Two Trillion,
The source code : SpellTest.java // SpellTest.java The link of the pastebin code is given in the second line of each source code. Each source file has a highlighted version in pastebin link which could be used alternatively. To test this project, copy these source files into the files with these file names in a single project, build the project and run it. SpellException.java : The exception class used for error reporting and handling.PatternDefinition.java : The class which helps to create a regular expression to validate the encoded text.SpellCode.java : The class which helps encoding and decoding the spelled text.SpellContext.java : The principle class which performs the action of spelling (a number) or parsing (a spelled number text).SpellTest.java : The main program of the project.In each case, it prints both the numeric value of the entered text (if it is a spelled number text or a numeric value) and the corresponding spelled text (likewise). The project provided here, is a demo program which lets user enter a text either containing a numeric value like 2010 or a spelled text like Two Thousand and Ten. The method which parses the spelled number text could parse a correctly spelled number text perfectly. By “tolerantly”, we mean that the function does not care much about the spelling grammar (of course, it cares about the spelling of the words, but it cares little about how these words are mixed.) It couldn’t be a great deal, because we could simply validate the text as descibed above using regular expression. The conversion from the spelled text to number is also possible using a bunch of functions which could do the task “tolerantly”. Then, the encoded text could be validated using regular expression.
Although it might look difficult, it is also possible to validate the grammar using some method of encoding which converts known number words like million, thousand, twenty, etc to a single character predefined code. It would be easy to check the spelling of the words. Moreover, is it possible to validate the user input text that is to decide whether the user input is a correctly spelled number (both regarding the spelling the words and the grammar of the spelling)? How about the reversed operation?! For example, giving “Nine Billion, Thirty Five Million, Seven Hundred and Fourteen Thousand and Twenty”, the function returns : 9,035,714,020. It is not done in a single function, but in a bunch of functions that most of them are recursive.
Actually, we have already done this part using C++ in the article: Spell Number: Read out a number in C++. It is possible to write a function to spell a number for example, giving 3409 as input, the function would return Three Thousand, Four Hundred and Nine. Java: A Simplified Demo for Spelling Numbers (link: ) See also a simplified version of demo program in the following article before testing this demo: