Results 1 to 20 of 32
Thread: Changing 8 to eight...need help!
 06182011, 07:24 AM #1Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
Changing 8 to eight...need help!
EDIT: About the confusion. I want to try and create a program that takes the input of "8" or any given number like "12,526" and returns to you "eight" or "twelve thousand five hundred twentysix." and visa versa, but that can come later
Have made no progress so far, don't really know where to start. This is a personal project of mine.
What I am thinking is just making a main string with a Scanner that will take the input of the number and run it through a bunch of if statements and then return something.
EDIT2: http://mindprod.com/applet/inwords.html
that link will help you grasp a sense of what I want to accomplish if you still don't understand it
Don't know where to start, or how to go about doing this. Some help as an outline or something would be greatly appreciatedLast edited by adjit; 06182011 at 07:41 AM.
 06182011, 07:29 AM #2Senior Member
 Join Date
 Apr 2010
 Location
 Philippines
 Posts
 580
 Rep Power
 5
Please note that title are NOT included or serves as a your question.
Please explain what you are trying to do, what have you done so far.
 06182011, 07:40 AM #3Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
Sorry, just included an EDIT in the main post
 06182011, 07:55 AM #4Senior Member
 Join Date
 Apr 2010
 Location
 Philippines
 Posts
 580
 Rep Power
 5
Is you converter accepts numbers with decimal entry? If yes, how long a decimal digit can get OR will you limit it to two decimal only?
Like 1587.98 is allowed but 1587.987 is not allowed?
 06182011, 08:07 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
Don't know where to start
On the other hand if it is this specific task of converting between numerals with digits and separators and words for the same number, then the place to start is by figuring out how you  rather than a computer  would convert between "12,526" and "twelve thousand five hundred twentysix". You need to be able to specify the recipe completely and precisely in order to know what it is that you are going to write as code.
Look for patterns that reoccur like the "twelve" that is part of "12,526" and "1,012".
Work on simpler variants of the problem first. Start with converting between "1">"9" and the corresponding words. Then extend that up to "99", perhaps by dealing with each digit separately.
 06182011, 08:45 AM #6Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
Well, I know how I would do simple numbers.
But do I have the right idea with the scanner and input 9 and have it print out nine?
Also, wouldn't I have to write essentially an infinite code in order to get it to work properly? because there can be an infinite number of inputs
 06182011, 09:02 AM #7
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
No, you can either use recursion or perform it iteratively, both ways a switch case will be helpful. Maybe it will be helpful to first split the numbers up without other words.
12562 would be twelve five sixty two.
Decide whether you would like to do this iteratively or recursively. Then think how you would pronounce the following numbers: 2, 20, 200, 2000, besides 20, what is the difference between these pronunciations? Would you append something to the changing part to pronounce the words correctly? Switch cases will be helpful here, the code will be very large, and there can be an infinite amount of code if you want(because there is an infinite amount of numbers).
For example, if you want to do numbers over a million, you need to handle millions, for billions you have to handle billion, etc. I suggest you start small and expand as you go.Last edited by sunde887; 06182011 at 09:05 AM.
 06182011, 09:08 AM #8Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
I'm relatively new to Java, took a 201 course, but I don't know what recursion is, so if you could point me in the right direction, it would be helpful
 06182011, 09:13 AM #9Senior Member
 Join Date
 Apr 2010
 Location
 Philippines
 Posts
 580
 Rep Power
 5
Formatting input into something like 100,000,000.00 will be helpful because you can use split() method. Then
you can use a for loop and check if you are converting hundred, thousand, million, billions and so on...
 06182011, 09:21 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
I answered this question before (including all code!); a forum search may cough it up.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 06182011, 09:21 AM #11Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
I thought split() was used on buffered files like a text file? or could you also use it for the input of something into the scanner?
also, would there be a way to have it so if I were to put in a number like 1,245 or 1245 (without the comma) that it would come out the same even if the split() method would be in place?
 06182011, 09:23 AM #12Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
 06182011, 09:32 AM #13
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
If you don't know what recursion is, you can either learn it with this problem, or stick to a string builder and iterative approach.
However, I'll give you a quick run down of what recursion is, there is a lot of material out there on the subject, and a book called "How to Design Programs"(freely available at htdp.org) is exceptionally good at helping you understand it(it doesn't use java however).
Recursion is a way of defining a method in terms of itself. For example the definition(in words) of summing an array recursively is
1. If the array is empty, return 0
2. The first item in the array + the method performed on the rest of the array
Arrays don't lend themselves perfectly to this definition since there is no easy way to get the rest of an array, but you can very easily keep track of the index currently worked on.
Java Code:public int sumArray(int index, int[] array){ if(index==array.length) return 0; else{ return array[index] + sumArray(index+1, array); } } public int sumArray(int[] array){ sumArray(0, array); }
I'll show an easier method to help show how the method looks on the stack
The factorial of a number n is defined as
1. If n is 1, return 1
2. n times factorial of n  1
In both of these example step 1 is a termination condition, you set up termination conditions to end the program, you must carefully have correct termination conditions or you will get infinite recursion and a stack overflow exception.
Java Code:public int factorial(int n){ if(n==1) return 1; else{ return n * factorial(n  1); } }
Java Code:factorial(5) 5 * factorial(4) 5 * 4 * factorial(3) 5 * 4 * 3 * factorial(2) 5 * 4 * 3 * 2 * factorial(1) termination condition reached 5 * 4 * 3 * 2 * 1 5 * 4 * 3 * 2 5 * 4 * 6 5 * 24 120
 06182011, 09:41 AM #14Senior Member
 Join Date
 Apr 2010
 Location
 Philippines
 Posts
 580
 Rep Power
 5
No, you need to format the input so you can use split. If you did not do it the
return will still 1234.
See this
Using Predefined Formats (The Java™ Tutorials > Internationalization > Formatting)
Customizing Formats (The Java™ Tutorials > Internationalization > Formatting)
 06182011, 09:53 AM #15
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
Suppose you want to transform the number xyz (where x, y, and z are digits) to words: note that you can do xhundredyz where you have to transform the (smaller) numbers x and yz to words; similar with the number xyztuv, it breaks down to xyzthousandthunderduv, etc. etc. The number zero is bit of an exception to this rule.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 06182011, 12:22 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
 06182011, 06:31 PM #17Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
[QUOTE=sunde887;216144]
Java Code:public int sumArray(int index, int[] array){ if(index==array.length) return 0; else{ return array[index] + sumArray(index+1, array); } } public int sumArray(int[] array){ sumArray(0, array); }
Java Code:int[] array
 06182011, 06:35 PM #18Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
So would I write this code by saying something that would say:
Java Code:if(the input number has 3 numbers){ System.out.println("hundred") } else if(the input number has 4 numbers){ System.out.println("thousand") }
Also, how would I break it down beyond that?
 06182011, 06:49 PM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
 06182011, 06:54 PM #20Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
Similar Threads

Changing binding when changing the underlying model object
By ChrisNY in forum NetBeansReplies: 0Last Post: 08142010, 10:09 AM 
help changing the RGB of a fillRect
By AnimeKitty in forum New To JavaReplies: 3Last Post: 07272010, 06:02 PM 
Changing Cursor
By ridvan in forum AWT / SwingReplies: 2Last Post: 06272010, 04:11 AM 
Changing the values of a map.
By Onra in forum New To JavaReplies: 1Last Post: 02262010, 01:20 AM 
Changing the Jframe
By Nemo1959 in forum New To JavaReplies: 13Last Post: 09192008, 03:58 PM
Bookmarks