Alex Gallo

Greenhorn

Posts: 4

Alejandro Loaiza

Greenhorn

Posts: 9

posted 4 years ago

for decimalDigit() i worked up:

switch ( index )

{

case 9: number /= 10;

case 8: number /= 10;

case 7: number /= 10;

case 6: number /= 10;

case 5: number /= 10;

case 4: number /= 10;

case 3: number /= 10;

case 2: number /= 10;

case 1: number /= 10;

case 0: return number % 10;

default: return 0;

I also erased the random there. Would this be correct coming from the Algorithm?

switch ( index )

{

case 9: number /= 10;

case 8: number /= 10;

case 7: number /= 10;

case 6: number /= 10;

case 5: number /= 10;

case 4: number /= 10;

case 3: number /= 10;

case 2: number /= 10;

case 1: number /= 10;

case 0: return number % 10;

default: return 0;

I also erased the random there. Would this be correct coming from the Algorithm?

Alejandro Loaiza

Greenhorn

Posts: 9

posted 4 years ago

I also worked on onesWord() and I got:

switch ( digit )

{

case 0: return "zero";

case 1: return "one";

case 2: return "two";

case 3: return "three";

case 4: return "four";

case 5: return "five";

case 6: return "six";

case 7: return "seven";

case 8: return "eight";

case 9: return "nine";

default: throw new RuntimeException("Non-Digit " + digit);

}

i erased the random there. would this be correct?

switch ( digit )

{

case 0: return "zero";

case 1: return "one";

case 2: return "two";

case 3: return "three";

case 4: return "four";

case 5: return "five";

case 6: return "six";

case 7: return "seven";

case 8: return "eight";

case 9: return "nine";

default: throw new RuntimeException("Non-Digit " + digit);

}

i erased the random there. would this be correct?

Alex Gallo

Greenhorn

Posts: 4

posted 4 years ago

It seems like it works (no errors), but that verse variable is still being brought up on the return.

Edit:

I removed the entire section of verse, but I dont know what I should set the return to for that part. If I do "words", then it gives me the correct digit in the ones, but that's it. (e.g 123 spells out just three)

Here is the new code: http://pastebin.com/u2KZumjN

Edit:

I removed the entire section of verse, but I dont know what I should set the return to for that part. If I do "words", then it gives me the correct digit in the ones, but that's it. (e.g 123 spells out just three)

Here is the new code: http://pastebin.com/u2KZumjN

Piet Souris

Master Rancher

Posts: 2044

75

posted 4 years ago

I suggest concentrating on getting the method

correct. All other methods depend on the digits that this method returns.

Take for example the number 321.

Now, determine how we would get the first digit, 3. This would be invoked by

The algorithm to do this is given in the comment, so let's use it:

divide 321 by 10, the number of times to do this being equal to the index.

So here goes:

321 / 10 = 32 (integer division, so the fraction is cut off)

32 / 10 = 3

So, we've done it two times, and we get 3 as a result. And it's correct!

Now, try to figure out how we could get the '2' when we invoke:

(although I must say that in the given algortihm step 2) is rather unclear to me)

correct. All other methods depend on the digits that this method returns.

Take for example the number 321.

Now, determine how we would get the first digit, 3. This would be invoked by

The algorithm to do this is given in the comment, so let's use it:

divide 321 by 10, the number of times to do this being equal to the index.

So here goes:

321 / 10 = 32 (integer division, so the fraction is cut off)

32 / 10 = 3

So, we've done it two times, and we get 3 as a result. And it's correct!

Now, try to figure out how we could get the '2' when we invoke:

(although I must say that in the given algortihm step 2) is rather unclear to me)

Tony Docherty

Bartender

Posts: 3271

82

Alejandro Loaiza

Greenhorn

Posts: 9

posted 4 years ago

can someone explain:

private static int decimalDigit(int index, int number)

{

//ALGORITHM:

//Step 1: Divide number by 10, index times

//Step 2: Find the remainder of number from Step 1 with 10

//Step 3: Return the remainder from Step 2

switch ( index )

{

case 2: number /= 10;

case 1: number /= 10;

case 0: return number % 10;

default: return 0;

private static int decimalDigit(int index, int number)

{

//ALGORITHM:

//Step 1: Divide number by 10, index times

//Step 2: Find the remainder of number from Step 1 with 10

//Step 3: Return the remainder from Step 2

switch ( index )

{

case 2: number /= 10;

case 1: number /= 10;

case 0: return number % 10;

default: return 0;

Tony Docherty

Bartender

Posts: 3271

82

posted 4 years ago

I think the methods

And in each case you have to write

In early post, I told you to erase all the Random codings. But you have left one in

The array verse is also an demonstration only. It is not necessary to the software.

**teenWord(int), tenWord(int)**, ... are there for return the String that gives the spelling to an input number. But you have done all of them in**threeDigitSpelling**method.And in each case you have to write

**word+=**but not*reading***word=**. That is one mistake.*reading*In early post, I told you to erase all the Random codings. But you have left one in

**threeDigitSpelling**method. You have to remove that and you should return**word**in that method as it has the spelling of the number.The array verse is also an demonstration only. It is not necessary to the software.

Ramesh-X

Alejandro Loaiza

Greenhorn

Posts: 9

posted 4 years ago

No, and you're concentrating too much on

My advice: Take several two- and three-digit numbers at random, write them out, and

You're

This is NOT a simple problem, so you really need to think

HIH

Winston

- 1

Alejandro Loaiza wrote:would applying switches to teenWord() and tensWord() still be beneficial?

No, and you're concentrating too much on

*code*. So: StopCoding (←click).

My advice: Take several two- and three-digit numbers at random, write them out, and

*really look at them hard*. What do they have in common? What

*don't*they?

You're

*some*way towards a solution, but you're not there yet; and some of what you have is using "brute force" rather than rational logic. There's nothing wrong with that as a 'first cut'; but I suspect you'll work out that there are ways to reduce the number of strings you actually need to do the job.

This is NOT a simple problem, so you really need to think

*before*you code.

HIH

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here