• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion  RSS feed

 
Kat Rollo
Ranch Hand
Posts: 62
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JavaRanch,

I found an open-source recipe for converting numbers to words.
I modified it a bit since the requirement is only for integers until 999:

From robosoul's response at StackOverflow, I simply inserted a condition for negative integers (line 12).
True enough, the code worked and I was able to trace (pen and paper method) how it is converting the 0 and positive integers to words.

However, I am a bit lost on how it is doing the conversion for negative integers.
Can someone kindly explain how it is successfully converting the negative?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all in line 12 - the conversion for "-X" is replaced by the conversion of "--X" - which is a positive number "X", and the word "negative" is output first. Seems pretty straightforward - which aspect of it has you confused?
 
Kat Rollo
Ranch Hand
Posts: 62
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf Dittmer,

For example, I have ans = -2.

I understand it will enter the first condition, if(ans < 0), and it will prefix "negative " to the String.
Then it will call convert(-2), which is the thing that is confusing me.

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then it will call convert(-2), which is the thing that is confusing me.

Actually, it will call "convert(- -2)" -note the minus sign in front of "ans"- which is the same as convert(2).
 
Kat Rollo
Ranch Hand
Posts: 62
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Right right, silly me.
Thanks a lot.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!