# Changing 8 to eight...need help!

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-18-2011, 07:24 AM
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 appreciated
• 06-18-2011, 07:29 AM
mine0926
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
Sorry, just included an EDIT in the main post
• 06-18-2011, 07:55 AM
mine0926
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
pbrockway2
Quote:

Don't know where to start
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.

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
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
sunde887
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.
• 06-18-2011, 09:08 AM
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
mine0926
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
JosAH
I answered this question before (including all code!); a forum search may cough it up.

kind regards,

Jos
• 06-18-2011, 09:21 AM
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
Quote:

Originally Posted by JosAH
I answered this question before (including all code!); a forum search may cough it up.

kind regards,

Jos

could you possibly give me something to go by for the search? or even a link please.
But also I want to do this on my own, so I understand it instead of just copying a previously posted code
• 06-18-2011, 09:32 AM
sunde887
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.

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);
}

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.

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.

Code:

public int factorial(int n){
if(n==1)
return 1;
else{
return n * factorial(n - 1);
}
}

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 forward
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
mine0926
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
JosAH
Quote:

could you possibly give me something to go by for the search? or even a link please.
But also I want to do this on my own, so I understand it instead of just copying a previously posted code

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,

Jos
• 06-18-2011, 12:22 PM
JosAH
Quote:

Originally Posted by sunde887
The factorial of a number n is defined as
1. If n is 1, return 1
2. n times factorial of n - 1

The pitiful mathematician in me must protest in the most strongliest way here:

The factorial of a number n is defined as
1. If n is 0, return 1
2. n times factorial of n - 1

kind regards,

Jos
• 06-18-2011, 06:31 PM
[QUOTE=sunde887;216144]
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);
}

So I kind of understand that, but I don't get the point of
Code:

int[] array
in both methods. What is that doing?
• 06-18-2011, 06:35 PM
Quote:

Originally Posted by JosAH
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,

Jos

So would I write this code by saying something that would say:
Code:

if(the input number has 3 numbers){
System.out.println("hundred")
}
else if(the input number has 4 numbers){
System.out.println("thousand")
}

should it look something like that in the end?
Also, how would I break it down beyond that?
• 06-18-2011, 06:49 PM
JosAH
Quote:

So would I write this code by saying something that would say:
Code:

if(the input number has 3 numbers){
System.out.println("hundred")
}
else if(the input number has 4 numbers){
System.out.println("thousand")
}

should it look something like that in the end?
Also, how would I break it down beyond that?

Yep, something like that; think of the modulo operator (%), e.g.

Code:

int xyz= 123;
int x= xyz/100;
int yx= xyz%100;

kind regards,

Jos
• 06-18-2011, 06:54 PM
Quote:

Originally Posted by JosAH
Yep, something like that; think of the modulo operator (%), e.g.

Code:

int xyz= 123;
int x= xyz/100;
int yx= xyz%100;

kind regards,

Jos

I kind of don't get that. What's the point of the modulo? Don't understand why you do xyz/100 and yx%100
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last