GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes reverse an integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reverse an integer" Watch "reverse an integer" New topic
Author

reverse an integer

nitin ratra
Greenhorn

Joined: Aug 11, 2008
Posts: 25
1234
how do i reverse this integer without in the simplest way
nitin ratra
Greenhorn

Joined: Aug 11, 2008
Posts: 25
sorry I posted this on servlets I should have posted it on beginners
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
SaurabhSri Sri
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi,

Try This -



[edit]Deleted part of the solution. CR[/edit]
[ August 23, 2008: Message edited by: Campbell Ritchie ]

Regards
SaurabhSri (SCJP 1.5)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
I failed to get a recursion to work, but it is easy with a while loop and the / and % operators.
Pravin Johnson
Greenhorn

Joined: Aug 23, 2008
Posts: 5
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 ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.
Hendy Setyo Mulyo
Ranch Hand

Joined: Dec 01, 2004
Posts: 219

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.


Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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
Greenhorn

Joined: Aug 11, 2008
Posts: 25
Thankyou everyone,hope I have asked the question in the right way this time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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.
 
GeeCON Prague 2014
 
subject: reverse an integer