0

How can i campare a textfield.settext with a string for example:

if(textField_1.setText != ("Hello"))

I can't do !=, how can i do it?

marcss
  • 253
  • 2
  • 14

3 Answers3

1

if(!textField_1.setText.equals("Hello"))

That's Java :)

you can use != and == to compare references and basic types (int, float ... )

In c++ we can overload the operator, but in java we have to use equals()

Kuba
  • 839
  • 7
  • 16
1

This is how the string comparision should be done.

if(textField_1.setText.equals("Hello")){

}

Update: I think you must use the get method to get the text, therefore I am expecting textField_1.getText() will give you the text to be compared.

Refer this

Community
  • 1
  • 1
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60
1

You need to use getText() method to compare.

if(textField_1.getText() != ("Hello")){
//not matched
}else{
//matched
}

But it is better to use equals() method to compare.

if(!textField_1.getText().equals("Hello")){
//not matched
}else{
//matched
}

Note That: == tests for reference equality.

.equals() tests for value equality.

Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24