-1

Below is a code that I have for flipping a given integer and displaying the flipped results. It runs but I have issues for when the number is smaller than two digits. It obviously cannot be flipped. I wanted to make the loop an if else stating "if number is two digits or more reverse." "Else state that the integer needs to be two or more digits." how could I go about this?

import java.util.Scanner;

public class ReverseInteger {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter an integer that you would like to have reversed: ");
        int number = input.nextInt();

        reverse(number);
    }

    public static void reverse(int userInteger)
    {
        int tempDigit = 0;

        while (userInteger > 0){

            tempDigit = userInteger % 10;
            System.out.print(tempDigit);
            userInteger = userInteger / 10;
        }
    }
}

I am trying to get it to understand that 01 can be converted to 10. This would need to be done by the code understanding that the userInteger is more than one digit but I cant seem to figure out how to do that... Any ideas on how I can get this to check for two digits and execute the loop accordingly would be appreciated!

mkobit
  • 43,979
  • 12
  • 156
  • 150
Eternal Vapor
  • 71
  • 3
  • 7
  • 15
  • 1
    You can test for `userInteger > 9`either for your special message or simply as the condition in the while loop (and then print the remaining number after the loop). `while(userInteger > 9) { .. } System.out.print(userInteger);` – eckes Apr 29 '15 at 02:50
  • 1
    **"when the number is smaller than two digits. It obviously cannot be flipped."** you are technically **wrong** ... reverse of 1 is 1 ! reverse of 2 is 2 . – Srinath Ganesh Apr 29 '15 at 02:52
  • I understand that it would flip to the same value I just wanted to make the code have a bit more logic in it that would make it seem more in depth in the matter of reversing the integer. – Eternal Vapor Apr 29 '15 at 02:55
  • "I am trying to get it to understand that 01 can be converted to 10." do NOT directly print to console ... append *tempDigit* to a String and Integer.pasrseInt(str) – Srinath Ganesh Apr 29 '15 at 02:56
  • @eckes I had that as a if statement before but found that I wanted to be able to flip numbers such as 05 to 50 which would fail the logic and do nothing at the current moment.Trying to accommodate a wider range of input options is really what I am trying to do I am just having trouble getting it to work. – Eternal Vapor Apr 29 '15 at 02:57
  • 2
    You cannot store "05" in an Integer. You would have to use a String, and then you can use string.length(). – eckes Apr 29 '15 at 02:59
  • You haven't actually computed the reversed integer value, so this is more a string processing problem. Could just do `System.out.print(new StringBuilder(input.next()).reverse())` – Zong Apr 29 '15 at 03:06
  • @EternalVapor **IF** you want your **input** to remain **exactly as 05** , THEN do the following => String input = ..... StringBuffer sb = new StringBuffer(input) .......... sb.reverse().toString() http://www.tutorialspoint.com/java/stringbuffer_reverse.htm – Srinath Ganesh Apr 29 '15 at 03:07

3 Answers3

1

I recommend using String.valueof(int).toCharArray(); and looping through in reverse to compose a new char[]. Then use Integer.parseInt(String);

ketan
  • 19,129
  • 42
  • 60
  • 98
Czipperz
  • 3,268
  • 2
  • 18
  • 25
1
public static void reverse(int n)
{
    int temp = 0;
    int count = 0;

    while(n != 0)
    {
        if(n%10 == 0)count++;
        temp = temp*10 + n %10;
        n /= 10;
    }
    for(int i = 0; i < count; i++)
    {
        System.out.print(0);
    }
    System.out.println(temp);
}
1

Convert the int to a String using Integer.toString method and save it to a string. Declare a String that will be later used as output. Start a for loop that goes through the number ( which was converted to a String) from its end to its beginning. It add each character from end to start to the output String. This results in the output String to be the reverse of the number String. Then just simply convert the output String using Integer.parseInt method and return the int value.

The code should look like:

public static int reverse(int n)
{
    String number = Integer.toString(n);
    String output;
    for(int i = number.length()-1; i >= 0; i--)
         output += number.charAt(i);
    return Integer.parseInt(output);
}
Rezwan Azfar Haleem
  • 1,198
  • 13
  • 23
  • I tried to use the code provided and edit to to work in my code but I get an error with String output; stating that it cannot be initialized. I tried to make it a value but it fauled to run then as well, I seethe login in your code and that seems to be exactly what I am looking for. Based on what @SrinathGanish said it needs to be a string to count the number of spaces. Then convert if possible. Any idea why the String output is failing? – Eternal Vapor Apr 30 '15 at 04:01
  • change `String output;` to `String output = "";` – Rezwan Azfar Haleem Apr 30 '15 at 12:33