Inferno
Inferno

Reputation: 57

What is the operator for "Less than or Equal to" in Java?

I am familiar with Actionscript programming, and I often used the "<=" (less than or equal to) or ">=" (greater than or equal to) operators.

However in Eclipse, I have been unable to use such operators. Here's my situation. Defined variable:

final EditText UserNumber = (EditText) findViewById(R.id.editText1);

And here's the use:

if (UserNumber <= 10){ }

I'm sure this is a very easy/quick fix but I have been unable to locate what should be used in this situation.

And this is the error I'm getting:

The operator <= is undefined for the argument type(s) EditText, int

Upvotes: 4

Views: 111503

Answers (5)

COD3BOY
COD3BOY

Reputation: 12092

As a solution, use this instead

Integer.parseInt(UserNumber.getText().toString()); 

In your case, this works fine,

 if((Integer.parseInt(UserNumber.getText().toString()) )<=10)
            {
               //Do what you want 
            }

Upvotes: 4

ncmathsadist
ncmathsadist

Reputation: 4891

Dangerous bend! You cannot use <= etc to compare Java objects. You need to use a compareTo method if it is implemented. This goes especially for strings.

Upvotes: 0

JohnCookie
JohnCookie

Reputation: 671

Integer.valueOf(UserNumber.getText().toString())<=10

Upvotes: 1

bencallis
bencallis

Reputation: 3658

You will first need to get the text from the edit text view, then if it is a Integer get the value from the string.

        Integer.parseInt(UserNumber.getText().toString()); 

As mentioned above.

Upvotes: 1

SLaks
SLaks

Reputation: 887245

As the error clearly states, you can't compare an EditText instance to a number.

You probably want to get the EditText's value.

Upvotes: 11

Related Questions