Reversing digits of an integer in Java

Greetings, esteemed experts!

I'm teaching myself Java (after decades of being a database guy). I'm going through a book, and one of the exercises is asking to write a method that reverses the digits of an integer and returns it as a String.

It sounded pretty simple, and I wrote code below, but the method isn't returning anything. This is probably extremely simple, but I'm missing it.

// Exercise 6.26:
// Reverse an integer string
package Chapter6;
import java.util.Scanner;

public class Exercise6_26 {
   public static void main( String[] args ) {
      int reverseThis = 0;
      while (reverseThis != -1) {
         Scanner input = new Scanner( );
         System.out.print( "Enter an integer (-1 to quit): " );
         reverseThis = input.nextInt();
         if (reverseThis == -1) break;

         System.out.printf("reversed(%d) = %s\n\n", reverseThis, reverseIt(reverseThis));
      } // while (side1 != -1)
   } // end main

   // reverse an integer and return a String
   public static String reverseIt( int reverseIt ) {
      StringBuilder sb = new StringBuilder(reverseIt);
      return sb.reverse().toString();
   } // end method reverseIt 

} // public class Exercise6_24

Open in new window

Here's my output:

Enter an integer (-1 to quit): 1234
reversed(1234) = 

Enter an integer (-1 to quit): -1

Open in new window

Thanks for your help!
LVL 18
Dave FordSoftware Developer / Database AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
You're calling this StringBuilder constructor:

You'll want to use Integer.toString() to turn that int into a String before you pass it to the StringBuilder constructor.
You can do it like this also :

class ReverseNumber {

static Integer number = 123456;
static Integer scratchNum = number;
static int workingint = 0;
static StringBuilder s = new StringBuilder();
static int n = 0;
static int m = 0;

  public static void main(String[] args){

for(int a=0;a<(int)number.bitCount(number);a++){

	n = (scratchNum.intValue()/10)*10;
	m = scratchNum.intValue();
	workingint = (m-n);
	scratchNum = new Integer(scratchNum.intValue()/10);




Open in new window

Tomas Helgi JohannssonCommented:

The constructor you are calling creates a empty string with the capacity of the size given in int.

In your orginal code you can modify your function like this.

public static String reverseIt( int reverseIt ) {
      StringBuilder sb = new StringBuilder(Integer.toString(reverseIt));
      return sb.reverse().toString();
   } // end method reverseIt

    Tomas Helgi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave FordSoftware Developer / Database AdministratorAuthor Commented:
Thank you! That worked beautifully.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.