CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Always returns false: (http://www.codingforums.com/showthread.php?t=269649)

Scriptr 08-06-2012 05:49 AM

Always returns false:
 
I have a method to test if a number is palendromic (the function is public boolean isPalen(int n))

It always returns false. Even if I change main to pass in a palendromic number directly.
Why? Can someone give me some tips?
Code:

public class Main {
        public static void main(String args[]){
                System.out.println("Starting....");
                Thread t1 = new Thread(){
                        public void run(){
                                new Main().loop(10,99);
                        }
                };
                t1.start();
                System.out.println("Done!");
        }
        public void loop(int l, int h){
                for(int ih = l; ih <= h; ih++){
                        for(int il = 10; il < 100; il++){
                                if(isPalen(ih * il))
                                        System.out.println("\t" + ih * il);
                        }
                }
        }
        public boolean isPalen(int n){
                String s1 = Integer.toString(n);
                String s2 = reverse(s1);
               
                return (s1 == s2);
               
        }
        public String reverse(String s) {
            return new StringBuffer(s).reverse().toString();
        }
}


Fou-Lu 08-06-2012 02:41 PM

return (s1 == s2); compares the memory space of two objects. Strings cannot be compared this way, you must use s1.equals(s2).


All times are GMT +1. The time now is 10:38 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.