954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

boolean return problem

Hey guys, I have one stupid problem with the return value of one function... this is my code:

private boolean check(String mail, int N){
                               // mail = The mail
                               // N = The sum of the characters before the @ and including it him
		if(mail.substring(N) == "hotmail.com" || mail.substring(N) == "live.com" ||
				mail.substring(N) == "msn.com" || mail.substring(N) == "yahoo.com" ||
				mail.substring(N) == "gmail.com"){
			return true;
		} else {
			return false;
		}
	}


...and if the mail is [email]daniweb@hotmail.com[/email] then mail.substring(N) will be hotmail.com but the return value will be false.

Where is my mistake?

Thanks.

Krstevski
Junior Poster
110 posts since May 2009
Reputation Points: 17
Solved Threads: 5
 

The == operator does not compare the characters of the string. It checks to see if they are referring to the same object. Use the mail.substring(N).equals("hotmail.com") method to compare the characters (or equalsIgnoreCase() I think).

nmaillet
Posting Whiz in Training
224 posts since Aug 2008
Reputation Points: 69
Solved Threads: 53
 

The problem is solved.

Thanks nmaillet. ;)

Krstevski
Junior Poster
110 posts since May 2009
Reputation Points: 17
Solved Threads: 5
 

ps

if (some boolean expression) {
   return true;
} else {
   return false;
}

is just the long way to say

return  (some boolean expression);
JamesCherrill
Posting Genius
Moderator
6,330 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You