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

6

5 Answers

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

You probably want to get the EditText's value.

4

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 } 
2

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.

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

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy