954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need urgent help with word to number conversion!

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!

<code>import java.util.*;
import java.io.*;

public class NumberWordFormat{
	Hashtable number;
	Hashtable decimalPlaces;

	public NumberWordFormat(){
		number=new Hashtable();
		decimalPlaces=new Hashtable();

		number.put(&quot;one&quot;,new Integer(1));
		number.put(&quot;two&quot;,new Integer(2));
		number.put(&quot;three&quot;,new Integer(3));
		number.put(&quot;four&quot;,new Integer(4));
		number.put(&quot;five&quot;,new Integer(5));
		number.put(&quot;six&quot;,new Integer(6));
		number.put(&quot;seven&quot;,new Integer(7));
		number.put(&quot;eight&quot;,new Integer(8));
		number.put(&quot;nine&quot;,new Integer(9));
		number.put(&quot;ten&quot;,new Integer(10));

		number.put(&quot;eleven&quot;,new Integer(11));
		number.put(&quot;twelve&quot;,new Integer(12));
		number.put(&quot;thirteen&quot;,new Integer(13));
		number.put(&quot;fourteen&quot;,new Integer(14));
		number.put(&quot;fifteen&quot;,new Integer(15));
		number.put(&quot;sixteen&quot;,new Integer(16));
		number.put(&quot;seventeen&quot;,new Integer(17));
		number.put(&quot;eighteen&quot;,new Integer(18));
		number.put(&quot;nineteen&quot;,new Integer(19));
		number.put(&quot;twenty&quot;,new Integer(20));

		number.put(&quot;thirty&quot;,new Integer(30));
		number.put(&quot;forty&quot;,new Integer(40));
		number.put(&quot;fifty&quot;,new Integer(50));
		number.put(&quot;sixty&quot;,new Integer(60));
		number.put(&quot;seventy&quot;,new Integer(70));
		number.put(&quot;eighty&quot;,new Integer(80));
		number.put(&quot;ninety&quot;,new Integer(90));

		decimalPlaces.put(&quot;hundred&quot;,new Integer(100));
		decimalPlaces.put(&quot;thousand&quot;,new Integer(1000));
		decimalPlaces.put(&quot;million&quot;,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(&quot;Invalid input! Please check your spelling!&quot;);
				System.exit(1);
			}
		}
		if(checkValid=true){
			String wordsArray[]=wordString.split(&quot; &quot;);
			int len=wordsArray.length;
			System.out.println(&quot;length: &quot;+len);

			for(i=len-1;i&gt;=0;i--){
				System.out.println(&quot;result: &quot;+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(&quot;i: &quot;+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(&quot;Please input number in words: &quot;);
			numbers=buffer.readLine();
			System.out.println(numbers+&quot; = &quot;+format.parse(numbers));
			System.out.println(&quot;Do you wish to enter another? [y/n]&quot;);
			answer=buffer.readLine();	
		}while(answer.equalsIgnoreCase(&quot;y&quot;));
	}
}</code>


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

sakura_fujin
Light Poster
27 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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.

orko
Junior Poster
164 posts since Apr 2006
Reputation Points: 46
Solved Threads: 11
 

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

sakura_fujin
Light Poster
27 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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: one : number, so add it: 0+1=1
read: hundred: decimal, so multiply it: 1*100=100
read: million: decimal, so multiply it: 100*1000000=100000000
...
... and so on.

orko
Junior Poster
164 posts since Apr 2006
Reputation Points: 46
Solved Threads: 11
 

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

xtianenikkian
Light Poster
39 posts since Sep 2008
Reputation Points: 11
Solved Threads: 3
 
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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You