File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes can anyone solve this ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can anyone solve this ?" Watch "can anyone solve this ?" New topic
Author

can anyone solve this ?

Kicky San
Greenhorn

Joined: Aug 24, 2005
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.


Cheers,<br />KicKy
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

I don't think you would get a question like this on the SCJP exam.

Is this question regarding the SCJP?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Kicky San
Greenhorn

Joined: Aug 24, 2005
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...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

There is no 'Homework' forum at the ranch

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


[My Blog]
All roads lead to JavaRanch
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Java in General (beginner) seems like the best bet, so I'm moving this there...


"I'm not back." - Bill Harding, Twister
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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 ]

Java API Documentation
The Java Tutorial
Tauri Valor
Ranch Hand

Joined: Aug 03, 2005
Posts: 166
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


A Moment's insight is sometimes worth a Life's experience.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
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];
}


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Also think about the results it gives you, its giving you 1 over on the thousands and ones.


Stephen Foy - Microsoft Application Development Consultant
Tauri Valor
Ranch Hand

Joined: Aug 03, 2005
Posts: 166
Kicky

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

Cheers,
Su Yeu.
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
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",
" 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.
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,


- Christian
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
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_


Mike Noel
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
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_
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anyone solve this ?