Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-06-2012, 05:49 AM   PM User | #1
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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();
	}
}
Scriptr is offline   Reply With Quote
Old 08-06-2012, 02:41 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
return (s1 == s2); compares the memory space of two objects. Strings cannot be compared this way, you must use s1.equals(s2).
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:20 PM.


Advertisement
Log in to turn off these ads.