Hi all I am completing an on-line exercise where I have to create a program that reads several paragraphs of text. All numbers (written in text) has to be added together and a sum shown at the end.
I have several questions if you guys dont mind answering as I have researched this quite a bit.
This is just an example of the text that is used:
eg Just remember that you're standing on a platform at twelve o'clock and there are five trains that run every hour on nine tracks. A train can go as fast as two hundred miles an hour. One of millions of billions...
I am using the StringTokenizer so each word is read individually.
From research I have learnt to create arrays that represent each word (please see code example), this is so the words are easily represented into ints. Though i have also create variables for each of the numbers, this is prob not required though I do not understand how Java can represent a word to its significant number. (again please see code).
Though my biggest problem is how would I go about combining words like two hundred and eigthy one = 281 using a loop.
Any advice would be greatly appreciated, I know this code is far from perfect as I am continuing to learn though online material and books.
class wordsToNumberAdder
{
public static void main()
{
String str = "Just remember that you're standing on a platform at twelve o'clock and there are
five trains that run every hour on nine tracks. A train can go as fast as two hundred miles
an hour. One of millions of billions... ";
StringTokenizer st = new StringTokenizer(str);
String[] digits = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String[] tens = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String[] teens = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String[] power = {"hundred","thousand","million","billion"};
int one = 1, two = 2, three = 3, four = 4, five = 5, six = 6, seven = 7, eight = 8, nine = 9;
int twenty = 20, thirty = 30, forty = 40, fifty = 50, sixty = 60, seventy = 70, eighty = 80, ninety = 90;
int ten = 10, eleven = 11, twelve = 12, thirteen = 13, forteen = 14, fifthteen = 15, sixteen = 16;
int eighteen = 18, nineteen = 19, thirty = 30, hundred = 100, thousand = 1000;
long billion = 1000000000;
double result = 0;
double group = 0;
while (set.hasMoreTokens()) {
String word = set.nextToken();
for (int x = 0; x < power.length; i += 3) {
if (word.equals(power[i])) {
group = group * Math.pow(10, i);
result = result + group;
group = 0;
}
}