-2

Possible Duplicate:
How do I compare strings in Java?

So, I have a question regarding the testing operators and strings. I'm trying to write a program that will take in user input of either "boy" or "girl". When the user inputs "boy", the output should be "You are a boy." When the user inputs "girl", the output should be "You are a girl."

However, when I compile and run the program, no matter what I input, the output is always "You are a girl."

Is this because the strings have no actual value like integers and therefore, testing operators cannot be used to compare them?

Also, is there anything like the assert function from python in java?

import java.util.Scanner;

class apples{
    public static void main(String args[]){
        System.out.println("Are you a boy or a girl?");
        Scanner lalala = new Scanner(System.in);
        String text = lalala.nextLine();

        if (text == "boy"){
            System.out.println("You are a boy.");
        }
        else{
            System.out.println("You are a girl.");
        }



    }
}

Thanks a lot~

Community
  • 1
  • 1
Edasaur
  • 387
  • 1
  • 7
  • 19

8 Answers8

6

Use

text.equals("boy")

instead of

 if (text == "boy")

The reason is, In Java, == always just compares two references (for non-primitives, that is) - i.e. it tests whether the two operands refer to the same object.

However, the equals method can be overridden - so two distinct objects can still be equal.

But the better option is to use equalsIgnoreCase, the reason is that user may enter boy or BOY or Boy or any other combination. equalsIgnoreCase method just ignores the case and compares two strings.

text.equalsIgnoreCase("boy")
Jayamohan
  • 12,469
  • 2
  • 26
  • 40
2

use text.equalsIgnoreCase("boy")

if you think about case also it will check content of string is same.

== is used to check for object equality.

text.equals("boy") is ok if you not consider about case.

someone
  • 6,419
  • 7
  • 34
  • 57
2

Side note. Input other than Boy will get you to print Girl. Put one more extra if condition. Also before going for comparision, being on safer side trim the string for blank spaces.

Use

 text.equals("boy")

or

 text.equalIgnoreCase("boy") 

if its not case sensitive.

instead of

if (text == "boy")

Your coade will be something like this

 import java.util.Scanner;

class apples{
public static void main(String args[]){
    System.out.println("Are you a boy or a girl?");
    Scanner lalala = new Scanner(System.in);
    String text = lalala.nextLine();
    text = text.trim();
    if (text.equalIgnoreCase("Boy")){
        System.out.println("You are a boy.");
    }
    else if(text.equalIgnoreCase("Girl")){
        System.out.println("You are a girl.");
    } else {
        System.out.println("Invalid Gender");
    }
 }
}
Vallabh Patade
  • 4,790
  • 5
  • 30
  • 40
1

Please use equals method for string like : if (text.equals("boy")){

== is not for string contents equality check in java

vishal_aim
  • 7,146
  • 1
  • 19
  • 23
1

== tests if object identity is the same. When you have two string objects containing the same value this object identity won't be equal. Use the equals function to test for logical equivalence.

Eelke
  • 19,500
  • 4
  • 48
  • 74
1

Your if else is wrong. Use only if here. Otherwise,

If I entered "some text", the output will be "You are a girl"

Like this

  if (text.equalsIgnoreCase("boy")) {
        System.out.println("You are a boy.");
    }
    else if (text.equalsIgnoreCase("girl")) {
        System.out.println("You are a girl.");
    } else {
        System.out.println("You are neither a boy nor a girl");
    }
Kim Kern
  • 43,715
  • 16
  • 163
  • 170
Mawia
  • 4,060
  • 12
  • 38
  • 55
0

It would be best to use the .equals method instead of == boy

brennaboo
  • 9
  • 1
  • 1
  • 8
0

For your other question regarding assert: Java has an assert function. This is the sample code.

  assert(x <= 50):      "Not Accepted"; 

This code checks if integer x is greater than 50. You will see the output at the console.

Jj Tuibeo
  • 753
  • 4
  • 18