Results 1 to 20 of 32
Thread: Changing 8 to eight...need help!
- 06-18-2011, 07:24 AM #1
Senior 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 twenty-six." 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; 06-18-2011 at 07:41 AM.
- 06-18-2011, 07:29 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
- 06-18-2011, 07:40 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
Sorry, just included an EDIT in the main post
- 06-18-2011, 07:55 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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?
- 06-18-2011, 08:07 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I don't know precisely what you mean by this. If you are saying that you don't know the basics of how to write a small Java program - defining a class and its methods, using basic control of flow constructs like loops and if/else - then the best thing to do is to put your immediate aim aside and go through a good Tutorial. Oracle offers The Java (tm) Tutorials, but there are many others in books or online.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 twenty-six". 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.
- 06-18-2011, 08:45 AM #6
Senior 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
- 06-18-2011, 09:02 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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; 06-18-2011 at 09:05 AM.
- 06-18-2011, 09:08 AM #8
Senior 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
- 06-18-2011, 09:13 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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...
- 06-18-2011, 09:21 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
I answered this question before (including all code!); a forum search may cough it up.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-18-2011, 09:21 AM #11
Senior 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?
- 06-18-2011, 09:23 AM #12
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
- 06-18-2011, 09:32 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
This method adds the terms in an array recursively. It basically breaks the problem into pieces and adds them together with recursive calls. This expands on the stack every time the method calls itself again.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.
This code will run until it reaches the recursive call, then it will add another method call to the stack. It looks like this as the program moves forwardJava 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
- 06-18-2011, 09:41 AM #14
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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)
- 06-18-2011, 09:53 AM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Suppose you want to transform the number xyz (where x, y, and z are digits) to words: note that you can do x-hundred-yz where you have to transform the (smaller) numbers x and yz to words; similar with the number xyztuv, it breaks down to xyz-thousand-t-hunderd-uv, etc. etc. The number zero is bit of an exception to this rule.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-18-2011, 12:22 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-18-2011, 06:31 PM #17
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
[QUOTE=sunde887;216144]
So I kind of understand that, but I don't get the point ofJava 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); }in both methods. What is that doing?Java Code:int[] array
- 06-18-2011, 06:35 PM #18
Senior Member
- Join Date
- Mar 2011
- Posts
- 171
- Rep Power
- 0
So would I write this code by saying something that would say:
should it look something like that in the end?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?
- 06-18-2011, 06:49 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 06-18-2011, 06:54 PM #20
Senior 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: 08-14-2010, 10:09 AM -
help changing the RGB of a fillRect
By AnimeKitty in forum New To JavaReplies: 3Last Post: 07-27-2010, 06:02 PM -
Changing Cursor
By ridvan in forum AWT / SwingReplies: 2Last Post: 06-27-2010, 04:11 AM -
Changing the values of a map.
By Onra in forum New To JavaReplies: 1Last Post: 02-26-2010, 12:20 AM -
Changing the Jframe
By Nemo1959 in forum New To JavaReplies: 13Last Post: 09-19-2008, 03:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks