Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# can anyone solve this ?

Kicky San
Greenhorn
Posts: 18
Hi Ranchers,
I recently came through this !! Can anyone post the code for this?
Here is the question :-
Get an input integer (from 0 to 99999).
The program should print the english equivalent for that integer.
for example, 17540 should get printed as seventeen thousand five hundred and forty.

> NO LIBRARY FUNCTIONS(FOR THIS LOGIC) SHOULD BE USED
> NO SQL STATEMENTS SHOULD BE USED.

Any help would be appreciated.

Mark Spritzler
ranger
Sheriff
Posts: 17278
6
I don't think you would get a question like this on the SCJP exam.

Is this question regarding the SCJP?

Mark

Kicky San
Greenhorn
Posts: 18
Sorry this is not concerned with SCJP. But where in this site should i post this question?
I just wanna know how to do that...

Sheriff
Posts: 14691
16
There is no 'Homework' forum at the ranch

But you may try at Java in General (beginner) forum.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Java in General (beginner) seems like the best bet, so I'm moving this there...

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Ok, as Satou already said, the forums here are not for having your homework done for you...

To solve this, first think about the problem yourself. How would you program something like this? Just think about the steps to solve the problem, without being concerned how it has to be put in Java code.

Here are some ideas to give you a start:

- You'll need a way to convert digits 0, 1, 2, ... to words: zero, one, two, ...
- You'll have to look at the tenthousands, thousands, hundreds, etc. of the number.
- Make a program that looks at all the digits of the number from left to right and convert them to text, digit by digit.

Layne Lund
Ranch Hand
Posts: 3061
I would suggest that you back up even further than Jesper says and figure out how YOU do this. When you see a number like 173, how do you go from the numerical representation to the words "one hundred seventy three"? Pick some other examples to help you understand how you convert numbers into words. Can you describe the steps you use in English or your native language?

Layne

p.s. Please use a more descriptive subject. Certainly there is someone that can solve this problem. However, a subject like "converting numbers into words" would make the intent of your post more clear.

Layne
[ January 12, 2006: Message edited by: Layne Lund ]

Tauri Valor
Ranch Hand
Posts: 181
Ok Kicky just try to find the bug in the following code..and you get the program...

public class NumberToWord {

private static final String[] ones = { " one", " two", " three", " four",
" five", " six", " seven", " eight", " nine", " ten", " eleven",
" twelve", " thirteen", " fourteen", " fifteen", " sixteen",
" seventeen", " eighteen", " nineteen" };

private static final String[] tens = { " twenty", " thirty", " forty",
" fifty", " sixty", " seventy", " eighty", " ninety" };

//
// so quintillions is as big as it gets. The
// program would automatically handle larger
// numbers if this array were extended.
//
private static final String[] groups = { "", " thousand", " million",
" billion", " trillion", " quadrillion", " quintillion" };

private String string = new String();

public String getString() {
return string;
}

public String EnglishNumber(long n) {

// Go through the number one group at a time.

for (int i = groups.length - 1; i >= 0; i--) {

// Is the number as big as this group?

long cutoff = (long) Math.pow((double) 10, (double) (i * 3));

if (n >= cutoff) {
int thisPart = (int) (n / cutoff);

// Use the ones[] array for both the
// hundreds and the ones digit. Note
// that tens[] starts at "twenty".

if (thisPart >= 100) {
string += ones[thisPart / 100] + " hundred";
thisPart = thisPart % 100;
}
if (thisPart >= 20) {
string += tens[(thisPart / 10) - 1];
thisPart = thisPart % 10;
}
if (thisPart >= 1) {
string += ones[thisPart];
}

string += groups[i];

n = n % cutoff;
}
}

if (string.length() == 0) {
string = "zero";
} else {
// remove initial space
string = string.substring(1);
}
return string;
}

public static void main(String args[]){
NumberToWord ntw = new NumberToWord();

String Englishword=ntw.EnglishNumber(2000);
System.out.println("English equivalentis :"+Englishword);

}

}

All the Best,
Su Yeu

Vishnu Prakash
Ranch Hand
Posts: 1026
Nice logic Su Yeu. I had the same assignment when I was in college.

Kicky san here is my tip

You got to concentrate in the below part to fix the bug

if (thisPart >= 20) {
string += tens[(thisPart / 10) - 1];
thisPart = thisPart % 10;
}
if (thisPart >= 1) {
string += ones[thisPart];
}

Stephen Foy
Ranch Hand
Posts: 143
Also think about the results it gives you, its giving you 1 over on the thousands and ones.

Tauri Valor
Ranch Hand
Posts: 181
Kicky

Did u get the bug fixed? Let me know if you have any issues.

Cheers,
Su Yeu.

Christian Nash
Ranch Hand
Posts: 107
public class NumbertoWord {

private static final String[] ones =
{
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen" };

private static final String[] tens =
{
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety" };

//
// so quintillions is as big as it gets. The
// program would automatically handle larger
// numbers if this array were extended.
//
private static final String[] groups =
{
"",
" thousand",
" million",
" billion",
" trillion",
" quintillion" };

private String string = new String();

public String getString() {
return string;
}

public String EnglishNumber(long n) {

// Go through the number one group at a time.
System.out.println("groups.length= " + groups.length);
for (int i = groups.length - 1; i >= 0; i--) {

// Is the number as big as this group?

long cutoff = (long) Math.pow((double) 10, (double) (i * 3));

if (n >= cutoff) {
int thisPart = (int) (n / cutoff);

// Use the ones[] array for both the
// hundreds and the ones digit. Note
// that tens[] starts at "twenty".

if (thisPart >= 100) {
string += ones[(thisPart / 100)-1] + " hundred";
thisPart = thisPart % 100;
}
if (thisPart >= 20) {
string += tens[(thisPart / 10) - 2];thisPart = thisPart % 10;
}
if (thisPart >= 1) {
string += ones[thisPart-1];}

string += groups[i];

n = n % cutoff;
}
}

if (string.length() == 0) {
string = "zero";
} else {
// remove initial space
string = string.substring(1);
}
return string;
}

public static void main(String args[]) {
NumbertoWord ntw = new NumbertoWord();

String Englishword = ntw.EnglishNumber(90);
System.out.println("English equivalentis :" + Englishword);

}

}

Cheers,

Mike Noel
Ranch Hand
Posts: 108
You guys make it too hard. I would have done:

The program would be real fast and you could easily extend it for a larger range of numbers (just requires more typing).

:-)

BTW, I used to teach an Intro to CS course and I would see answers like this...

_M_

Garrett Rowe
Ranch Hand
Posts: 1296
Maybe you can draw some inspiration from this guy, apparently he's done it for a number of different languages and number formats.
[ January 19, 2006: Message edited by: Garrett Rowe ]

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Mike, I've seen that kind of brute force approach used to great effect.

A friend wrote a date manipulation routine this way. In COBOL it's easy to take part of a string or number as another number, like pulling 0119 out of 20060119. He hand typed a table with 1231 entries. To see if 0119 is valid, just pull the entry at that index. Entry 119 says it's a valid day, also has the day of the month, quarter and year in it. The only special case is 0229 and for that you check if YYYY is a leap year. By getting entry 2006 from the leap year table of course. The program had about 8,000 lines of data and 400 lines of code for a couple dozen different functions.

Measuring quality depends on your priorities. His top priority was the shortest possible execution path and it was excellent by that measure.

Mike Noel
Ranch Hand
Posts: 108
Yeah, I have too. Sometimes that's exactly what's called for. Isn't that the essence of lookup tables? Most of the time we could code the logic that creates the table but it's much faster to precompute and lookup the cached answer.

Of course in the case of a homework assignment the brute force approach isn't going to score too well in the design category.

_M_