This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Recursion

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to write this segment of code with type long.
 
author & internet detective
Posts: 39530
776
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To begin with, if you are trying to reverse a String, the method should take a String parameter.

Can you think of what the base case should be for a String? What is the smallest String?
 
Ana Smith
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:To begin with, if you are trying to reverse a String, the method should take a String parameter.

Can you think of what the base case should be for a String? What is the smallest String?


Our teacher gave us the method  header. So apparently it's supposed to be type long
 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a long then you can just use integer division and the % operator to "tear off" each digit from the right end of the number, concatenating each one to a string result.

Here's an example:

156 % 10 == 6 --> concatenate to result: "6"
156 / 10 == 15

15 % 10 == 5 --> concatenate to result: "65"
15 / 10 == 1

1 % 10 == 1 --> concatenate to result: "651"
1 / 10 == 0

So reverse of the long value 156 is "651" as a String

You should be able to figure out from that example what kind of operations you need and how to iterate over any long value.
 
Marshal
Posts: 7178
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

I don't see any recursive calls in your code. Is it the same code you posted here and executing?

I see two method calls: [1] reverse and [2] reverseString. From what I see, that has nothing to do with recursion.
 
Ana Smith
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles now but it doesn't print out the correct number and it has a stackOverFlowError


 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run through the algorithm you coded and see if it ever is the case that x < 0. If not, that's the reason you're blowing your stack. To terminate the recursion, you need to find the correct condition for your base case.

Hint: It looks to me like you're almost there. Just that base case condition to work out.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!