• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can anyone solve this ?

 
Kicky San
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no 'Homework' forum at the ranch

But you may try at Java in General (beginner) forum.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java in General (beginner) seems like the best bet, so I'm moving this there...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also think about the results it gives you, its giving you 1 over on the thousands and ones.
 
Tauri Valor
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kicky

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

Cheers,
Su Yeu.
 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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_
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic