Wrong result testing integer input for palindrom-ness

Java - I should save an entered number in an array and should test this wether it is a palindrom or not. My code seems to be wrong, but I don`t know why. Can you help please? Thank you.

public static void main(String[] args) {

    // Zahleingabe
    int input = readInt("Bitte geben Sie eine positive Zahl ein:");
    while(input < 0) {
        input = readInt("Bitte geben Sie eine positive Zahl ein:");
    }

    // Array anlegen
    int[] inputArray = new int[input];

    // Array zerlegen
    int i = 0;
    int j;
    while(input / 10 == 0) {
        j = input % 10;
        inputArray[i] = j;
        input = input / 10;
    }
    int[] newInputArray = new int[inputArray.length];
    i = 0;
    while(i < newInputArray.length-1) {
        newInputArray[i] = inputArray[i];
        i++;
    }

    // Auf Palindrom Eigenschaft überprüfen
    i = 0;
    j = newInputArray.length-1;
    while(i < j) {
        if(newInputArray[i] != newInputArray[j]) {
            write("Die Zahl " + input + " ist kein Palindrom.");
            return;
        }
        i++;
        j--;
    }
    write("Die Zahl " + input + " ist ein Palindrom.");     

}

Edit's from commments:

  • I am not allowed to convert the integer into a string and test that - I have to use an Array that holds each digit of my input and check that.
  • My problem / questions: the code does not work - it tells me 122 is a palindrom which it is not, I have no clue why.
  • I am not allowed to use Math.XXX functions (log10, pow, ...)

1 answer

  • answered 2017-11-12 20:32 Patrick Artner

    Pseudocode:

    • get input // 121
    • calculate arrayDim = ((int)Math.log10(121))+1 // 3
    • create an array of that size.
    • Loop from power=arrayDim-1 down to power = 0 // 2,1,0

      • calculate powerForDivide = 10^power // 100, 10, 1
      • calculate digit = inputNumber / powerForDivide (use integer div, no decimals) // 1, 2, 1
      • put digit into array at arrayDim-(power+1) // [1,2,1]
      • reduce inputNumber by powerForDivide*digit // 121 - 100, 21-20, 1-1
    • test your array

    The values after // denote the values in the different loops for a given Input of 121.

    Code:

    public class JavaFiddle
    {
        // get the amount of digits (==dimensions) to store an integer n
        public static int getDims(int n)
        {
            if (n < 10) return 1;
    
            return 1 + getDims(n/10) ; 
        } 
    
        // helper to calculate 10^p
        public static int tenToPower(int p)
        {
            if (p < 1) return 1;
    
            return 10 * tenToPower(p-1);
        }
    
        public static void main(String[] args)
        {
            // removed console input so it works with https://javafiddle.leaningtech.com/
            int input =  12255221;
    
            int arrayDim = getDims(input); // ((int)Math.log10(input))+1;
            System.out.println("arrayDim(" + input + ") = " + arrayDim);
    
            // Array anlegen
            int[] inputArray = new int[arrayDim];
    
            int i = arrayDim;
            int rest = input;
            int powerForDiv = tenToPower(i-1); // (int)Math.pow(10,i-1);
    
            while(i > 0) {
                i = i - 1;
                // System.out.println(powerForDiv);
    
                int digit = rest / powerForDiv;
                int index = arrayDim - i - 1;
    
                System.out.println(digit + " at " + index); 
    
                rest = rest - digit*powerForDiv;
                inputArray[arrayDim - i - 1] = digit; 
                powerForDiv =  powerForDiv / 10;
             } 
    
             System.out.print("[");
             for ( int idx = 0; idx < arrayDim; idx++) { 
                System.out.print(inputArray[idx]);
                if (idx <arrayDim-1) 
                    System.out.print(":");
                else
                    System.out.println("]"); 
             } 
    
            i = 0;
            int j = inputArray.length-1;
            while(i < j) {
                if(inputArray[i] != inputArray[j]) {
                    System.out.println( input + " no Palindrom.");
                    return;
                }
                i++;
                j--;
            }
    
            System.out.println( input + " is Palindrom.");
        }
    }