Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 palindrom
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: 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 = ""; 
                        }
                }
        }
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Jul 01, 2006 11:53 am   Post subject: (No 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.
cool dude




PostPosted: Sat Jul 01, 2006 1:16 pm   Post subject: (No subject)

oh yeh. totally forgot. thanks Smile
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: