• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

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: Exercise6_26.java
// 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.in );
         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!
Dave Ford
Dave Ford
2 Solutions
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
Dave FordSoftware Developer / Database AdministratorAuthor Commented:
Thank you! That worked beautifully.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now