0

This is part of a longer coding challenge - one part involves "flipping" the digits of an input number (i.e. 1234 becomes 4321) and removing leading zeros as applicable.

Below, I have written the method flipOpp that accomplishes this. Most of the time, it works perfectly. But sometimes, I'm getting an error because the last digit becomes a dash ("-") and obviously, the Integer.parseInt() method won't work if one of the digits is a dash!

Any ideas what might be causing this? Also, is there an easier way to flip the digits of an int? The method I'm using right now doesn't seem very efficient - turning an int to a String, then to a character array, manipulating the characters of this array, turning it back into a String, and finally back to an int.

Thanks! Code for this method is below:

// third operation: reverse the digits and remove leading zeros
    public static int flipOpp(int num){
        char temp;
        // change int to a String
        String stringNum = Integer.toString(num);
        // change String to a char array of digits
        char[] charNum = stringNum.toCharArray();
        // flip each character and store using a char temp variable
        for (int i=0;i<charNum.length/2;i++){
            temp = charNum[i];
            charNum[i]=charNum[charNum.length-i-1];
            charNum[charNum.length-i-1]=temp;
        }
        // turn flipped char array back to String, then to an int
        // this process removes leading zeros by default
        String flipString = new String(charNum);
        if (flipString.length()<7){
            int flipInt = Integer.parseInt(flipString);
            return flipInt;
        }
        else return 0;

    }
Seth
  • 13
  • 3
  • Reversing a string is described is this [post](https://stackoverflow.com/questions/7569335/reverse-a-string-in-java). – Jack G. Jun 18 '17 at 16:54
  • "Any ideas what might be causing this?" this what? –  Jun 18 '17 at 16:54
  • 5
    Yes. Negative numbers are causing this. – Michael Markidis Jun 18 '17 at 16:55
  • You shouldn't be using a string at all, really. https://stackoverflow.com/questions/16392459/recursion-digits-in-reverse-order and https://stackoverflow.com/questions/6317446/which-of-recursion-method-is-better-and-why-for-reverse-digits-of-integer – OneCricketeer Jun 18 '17 at 16:57
  • 1
    Is code really supposed to return `0` if `num` is greater than `999999`? – Andreas Jun 18 '17 at 16:57
  • Andreas - yes, it has to do with the rest of the problem (I can largely ignore numbers larger than 4 digits...I choose 6 fairly arbitrarily). Michael - negative numbers? Unless there's a big error elsewhere in my code, that shouldn't be an issue...the 3 possible operations are 1) adding 5, 2) multiplying by 3, or 3) reversing the digits, as I've done above. Also, the dash appears at the end of the digits, i.e. 4589- not at the beginning – Seth Jun 18 '17 at 19:42

1 Answers1

0

Any ideas what might be causing this?

Definitely sounds like negative numbers

is there an easier way to flip the digits of an int? The method I'm using right now doesn't seem very efficient

Keep it as an integer. Don't worry about the negative

public static int flipOpp(int num) {
    int reversed = 0;

    while (num!=0) {
        reversed = reversed*10 + num%10; 
        num /= 10;   
    } 
    return reversed;
} 

For example, -50,

0*10+0=0
-50/10=-5
- - 
0*10+(-5)=-5
-5/10=0
- - 
END, output -5
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245