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

98,526

Solution 1

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

You probably want to get the EditText's value.

Solution 2

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 
            }
Share:
98,526
Inferno
Author by

Inferno

Updated on July 09, 2022

Comments

  • Inferno
    Inferno almost 2 years

    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

  • Inferno
    Inferno about 12 years
    Thanks everyone! And ya, oops I meant JAVA! :) Also, so I take it the operator is still the same, it's just I was using it to compare two irrelevant things?
  • SLaks
    SLaks about 12 years
    Exactly. Java is not an acronym.
  • Inferno
    Inferno about 12 years
    lol sheesh so literal! Also, I forgot to mention that the "UserNumber" IS in fact a number, but it's in a "edit text" box in the GUI.
  • Louis Wasserman
    Louis Wasserman about 12 years
    It's not a number. It's a text box that contains a string that represents a number. You'll need to use Integer.parseInt(String) to turn the String into an int.
  • Inferno
    Inferno about 12 years
    Awesome! Thanks a lot! But for some reason it's still giving me the same error. Theoretically, it should be converting the number entered by the user (read as a string of text by the computer), into a useable/comparable integer, right?
  • Inferno
    Inferno about 12 years
    Oh thanks lol! Wow I think I need to go to bed if I didn't even realize to include the operator! Thanks everyone for your help! I swear I spend the most amount of time on the simplest things!
  • COD3BOY
    COD3BOY about 12 years
    @Inferno parseInt parses the string argument as a signed decimal integer. See the edit if you used it like that, this worked for me, and if you still get the error, the error is something else. pls check your code once again. cheers :)