Computer Science Canada

palindrom

Author:  cool dude [ Sat Jul 01, 2006 11:03 am ]
Post subject:  palindrom

this is wierd because it doesn't show any error in my code, but it doesn't show any output either. although i know that the output should be 9009 because 91 x 99 = 9009 and that is a palindrom since its the same number backwards.

code:

public class Problem4 {
        public static void main (String[] args) {
                String palindrom = "";
                int product = 0;
                String product2 = "";
               
                for (int j = 10; j <= 100; j++){
                        for (int i = 10; i <= 100; i++){
                                product = j * i;
                                product2 = Integer.toString(product);
                                for (int k = 0; k < product2.length(); k++) {
                                        palindrom = product2.substring(k, k + 1) + palindrom;
                                }
                                if (product2 == palindrom){
                                        System.out.println(product2);
                                }
                                palindrom = ""; 
                        }
                }
        }
}

Author:  wtd [ Sat Jul 01, 2006 11:53 am ]
Post subject: 

You're trying to compare Strings with ==. This is probably not going to do what you expect. Once again, objects and native types in Java behave differently, and String is an object type.

Author:  cool dude [ Sat Jul 01, 2006 1:16 pm ]
Post subject: 

oh yeh. totally forgot. thanks Smile


: