Palindrom

mail@pastecode.io avatar
unknown
java
2 years ago
1.4 kB
2
Indexable
Never
package com.ljipp;

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        int x = 133331;

        if (x < 0) {
            System.out.println("Broj je negativan! Nije palindrom.");
        }

        ArrayList<Integer> lista = new ArrayList<Integer>();

        while (x > 0) {
            System.out.println(x);
            System.out.println(x % 10);
            lista.add(x & 10);
            x = x / 10;
        }

        boolean isPalindrome = true;
        for (int i = 0; 1 < lista.size() / 2; i++) {

            if(lista.get(i) != lista.get(lista.size() - i - 1)) {
                isPalindrome = false;
        }
        if (isPalindrome == false) System.out.println("Nije palindrom!");
        else System.out.println(" Palindrom je");
    }
}
}

// Greške koje dobivam 
// Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 6 out of bounds for length 6
//	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
//	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
//	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
//	at java.base/java.util.Objects.checkIndex(Objects.java:359)
//	at java.base/java.util.ArrayList.get(ArrayList.java:427)
//	at com.ljipp.Main.main(Main.java:26)