Q:

JAVA if statement problems

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!" );
        }
     }
 }    

A:

Use equals() method to compare String
equals() compares the object
== compares the references value
Use
if ("boy".equals(a)) {

This will compare "boy" instance of String with String instance referred by a

See

Java String.equals versus ==

A:

If you were to say:
 if ("boy".equals(a)){
 System.out.print("You are a male");
 } else if ("girl".equals(a)){
 System.out.print("you are a female!");
 { else {
 System.out.print("invalid response!");
 }

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.

