-2

I have performed some tests, when I switch this section to isPali = True:

else{ 
    isPali = false;
    p = n;
} 

All my numbers tested are considered palindromes. So for some reason any numbers I have above two characters are simply being dumped into the else. I tried desk checking but have been unable to determine why.

Output when run normally is 0 to 9 then nothing.

public class Palindromorizor{
    public static void main (String[] args){
        String inputString, limit; 
        int n, p, numLim, num = 0;
        boolean isPali;
        limit = JOptionPane.showInputDialog("What is your limit?");
        numLim = Integer.parseInt(limit);

        while(num < numLim){
            isPali = isPalindrome(num);

            if(isPali == true){
                System.out.print(num + " ");
                ++num;
            }
            else{
                ++num;
            }
        }
    }

    public static boolean isPalindrome(int num){
        String strNum;
        int n = 0;
        int p;
        boolean isPali = true;
        strNum = "" + num;
        p = strNum.length();

        //strNum.substring(p - 1, p) + " " + p + " " + n + " " + strNum + " " + isPali);

        while(p > n){
            if(strNum.substring(n, n + 1) == strNum.substring(p - 1, p) && (p <= n)){
                isPali = true;
            }
            else if(strNum.substring(n, n + 1) == strNum.substring(p - 1, p) && (p > n)){
                ++n;
                --p;
            }
            else{
                isPali = false;
                p = n;
            }
        }

        return isPali;
    }
}
Gimongi
  • 13
  • 1
  • 3

1 Answers1

0

Because you are comparing Strings with == operator. Use equals method.

Michal Przysucha
  • 1,021
  • 11
  • 13