This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
stewards:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

reverse an integer

 
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1234
how do i reverse this integer without in the simplest way
 
nitin ratra
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I posted this on servlets I should have posted it on beginners
 
pioneer
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one way (some details have been omitted).



[ August 23, 2008: Message edited by: Garrett Rowe ]
[ August 23, 2008: Message edited by: Garrett Rowe ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's an easier way than what Garrett suggests. Hint: Look at the API documentation of class StringBuilder (or StringBuffer if you're using Java 1.4 or older) and see if there's a method there that you can use.
 
pioneer
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try This -



[edit]Deleted part of the solution. CR[/edit]
[ August 23, 2008: Message edited by: Campbell Ritchie ]
 
Marshal
Posts: 68007
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't go giving out answers like that, SaurabhSri Sri. Read this FAQ. Nitin will learn lots more if you do as Garrett did, leaving out part of the solution to be worked out. I have felt obliged to delete part of your post.

By the way: Use StringBuilder rather than StringBuffer unless you are constrained to use Java 1.4.2 or older.
 
Campbell Ritchie
Marshal
Posts: 68007
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is to set up a recursion using the integer divide / and remainder % operations. I would prefer that way for an exercise myself.
 
Campbell Ritchie
Marshal
Posts: 68007
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I failed to get a recursion to work, but it is easy with a while loop and the / and % operators.
 
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this code to reverse an integer..

class reverse
{
[removed - DOM]
}
[Please see Campbell's post above. - Dave]
[ August 23, 2008: Message edited by: David O'Meara ]
 
pollinator
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Another way is to set up a recursion using the integer divide / and remainder % operations. I would prefer that way for an exercise myself.



This would be my approach, but I like the String based soluions too.
 
pioneer
Posts: 219
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String based solution is definitely the easiest. It suits more if you want to reverse a very very long number such as 12345678901123456656795943456989767. That kind of very very long number is cannot be handled by any Java numeric data type.
 
Campbell Ritchie
Marshal
Posts: 68007
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Hendy Setyo Mulyo:
12345678901123456656795943456989767. That kind of very very long number is cannot be handled by any Java numeric data type.



I shall have to try it with BigInteger.
 
pollinator
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Another way is to set up a recursion using the integer divide / and remainder % operations. I would prefer that way for an exercise myself.



In the old Tenex world, that was a common code sample fragment. At first, it was called "decout" (for decimal out) and you could change the constant that you do the div and mod by to convert it to octal, hex, etc.

later jargon file called it yornp for Ye Olde Recursive Number Printer.

Anyone claiming to speak PDP-10, Tenex, TOPS-10 or TOPS-20 was expected to know it by heart.
 
nitin ratra
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou everyone,hope I have asked the question in the right way this time.
 
Campbell Ritchie
Marshal
Posts: 68007
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
I failed to get a recursion to work, but it is easy with a while loop and the / and % operators.

Got it, with recursion. Nitin, have you got it to work; you are the one person who is allowed to show a complete answer.
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
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!