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.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{
NumberWordFormat format=new NumberWordFormat();
String numbers;
do{
System.out.println("Please input number in words: ");
System.out.println(numbers+" = "+format.parse(numbers));
System.out.println("Do you wish to enter another? [y/n]");
}
}
``````

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 by diafol: fixed formatting

5
Contributors
6
Replies
8
Views
10 Years
Discussion Span
Last Post by Ezzaral

suggestion: start from scratch and think before you hack code together.

And you'd better hurry or you'll never get done in time.

And no, we're not going to do it for you.
Indeed it's not urgent to us at all.

sakura@ Can you tell us what you are actually trying to do in your code? I mean you did not mention if you are getting any exception or wrong output or whatsoever. If you can tell us the problem you are facing precisely, we will be able to help you.
Thanks.

my problem: the code doesn't know how to differentiate 'hundred' from one hundred and 'hundred' from one hundred thousand or hundred million. when it sees the word 'hundred', it automatically multiplies 100 to the current result variable, which leads me to a wrong output. our prof extended our deadline so i guess i will still be working with it, with or without help although i guess it will be a bonus for me to have... thanks

I just compiled and ran your code. You said for one hundred thousand you get wrong output, whereas I can see you are not supposed to get any output at all but a null pointer exception. Fix your logic for parsing data. If you change your logic inside if(checkValid=true){ code }, you should be fine. One way is to remember the previous value, multiply with the decimals or add them with the numbers.
So, for one hundred million:
initial= 0;
read: hundred: decimal, so multiply it: 1*100=100
read: million: decimal, so multiply it: 100*1000000=100000000
...
... and so on.

the one you create has 100 error on my jcreator.,,
im uusng jdk1.5.0_16 file.,,, can you reply me with running program?

the one you create has 100 error on my jcreator.,,
im uusng jdk1.5.0_16 file.,,, can you reply me with running program?

Then fix the errors. If you want to copy code from others, you assume the risk that it's bad code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.