hi, i need urgent help with my code. My program, which converts word strings to its number equivalent, only works with special cases.

Example: "nine hundred ninety nine thousand nine hundred ninety nine" outputs 999999. (which is correct.) But try inputting "nine million nine hundred ninety nine thousand nine hundred ninety nine", it outputs 108001899 or "one thousand one hundred twenty five" gives 26100. Range is from -999,999,999 to 999,999,999. I'm still working with negative inputs though. And I think I might've just forced 999999 case to work by adding the "if(i==5)" part. HELP!

```
import java.util.*;
import java.io.*;
public class NumberWordFormat{
Hashtable number;
Hashtable decimalPlaces;
public NumberWordFormat(){
number=new Hashtable();
decimalPlaces=new Hashtable();
number.put("one",new Integer(1));
number.put("two",new Integer(2));
number.put("three",new Integer(3));
number.put("four",new Integer(4));
number.put("five",new Integer(5));
number.put("six",new Integer(6));
number.put("seven",new Integer(7));
number.put("eight",new Integer(8));
number.put("nine",new Integer(9));
number.put("ten",new Integer(10));
number.put("eleven",new Integer(11));
number.put("twelve",new Integer(12));
number.put("thirteen",new Integer(13));
number.put("fourteen",new Integer(14));
number.put("fifteen",new Integer(15));
number.put("sixteen",new Integer(16));
number.put("seventeen",new Integer(17));
number.put("eighteen",new Integer(18));
number.put("nineteen",new Integer(19));
number.put("twenty",new Integer(20));
number.put("thirty",new Integer(30));
number.put("forty",new Integer(40));
number.put("fifty",new Integer(50));
number.put("sixty",new Integer(60));
number.put("seventy",new Integer(70));
number.put("eighty",new Integer(80));
number.put("ninety",new Integer(90));
decimalPlaces.put("hundred",new Integer(100));
decimalPlaces.put("thousand",new Integer(1000));
decimalPlaces.put("million",new Integer(1000000));
}
public int parse(String wordString){
int i;
int result=0;
int quantity=1;
boolean changedQuantity=false;
boolean checkValid = false;
StringTokenizer tokens =new StringTokenizer(wordString);
while(tokens.hasMoreTokens()){
String current=tokens.nextToken();
if(number.containsKey(current)){
checkValid =true;
}else if(decimalPlaces.containsKey(current)){
checkValid=true;
}else{
System.out.println("Invalid input! Please check your spelling!");
System.exit(1);
}
}
if(checkValid=true){
String wordsArray[]=wordString.split(" ");
int len=wordsArray.length;
System.out.println("length: "+len);
for(i=len-1;i>=0;i--){
System.out.println("result: "+result);
if(!changedQuantity){
if(decimalPlaces.containsKey(wordsArray[i])){
quantity=((Integer)decimalPlaces.get(wordsArray[i])).intValue();
changedQuantity=true;
continue;
}
}
if(i==5){
quantity=quantity*1000;
System.out.println("i: "+i);
}
result+=quantity*((Integer)number.get(wordsArray[i])).intValue();
changedQuantity=false;
}
}
return result;
}
public static void main(String args[]) throws IOException{
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
NumberWordFormat format=new NumberWordFormat();
String numbers;
String answer;
do{
System.out.println("Please input number in words: ");
numbers=buffer.readLine();
System.out.println(numbers+" = "+format.parse(numbers));
System.out.println("Do you wish to enter another? [y/n]");
answer=buffer.readLine();
}while(answer.equalsIgnoreCase("y"));
}
}
```

I need to hand this program over by tomorrow afternoon and still stuck with the bug. all comments/suggestions/troubleshooting will be appreciated. thanks

*Edited 3 Years Ago by diafol*: fixed formatting