Possible Duplicate:
Java String.equals versus ==
I am using an if statement in java to determine weather the person is a male or female by asking weather he is a boy or a girl. This is quite a stupid statement but my query is no matter what i input i always get “you are a female!” which is quite annoying. Could you please help? This is the code
import java.util.Scanner;
class ifstatement {
public static void main( String args[] ) {
System.out.print( "please enter boy or girl as an input:" );
Scanner x = new Scanner( System.in );
String a = x.nextLine();
if ( a == "boy" ) {
System.out.print( "You are a male" );
}
else {
System.out.print( "You are a female!" );
}
}
}
If you were to say:
This would solve your problem. When dealing with strings you should always use .equals() to compare exact values. the “==” operator compares whether two objects point to the same location in memory generally. Since a string is an object they are not the same.