How can you compare strings in android with greater than

15,787

Solution 1

You'll need to convert the String to a number first. Something like this:

int number = Integer.parseInt(number.getText().toString());
if (number > 9)
{
    output.setText("50");
}

If the String is not guaranteed to be a valid integer you'll have to handle NumberFormatException too.

Solution 2

Is there a reason you can't use

Integer.valueOf("9");
Share:
15,787
Daniel
Author by

Daniel

Updated on June 13, 2022

Comments

  • Daniel
    Daniel almost 2 years

    I was wondering if there is a way to compare strings in android with greater than or >.

    Lets say I have this:

    String numbers = number.getText().toString();
    if (numbers.equals("9")){
    output.setText("50");}
    

    so if you enter 9 in the number EditText field the output TextView will display 50.
    I have quite a few different numbers that will then = a different number but what can I do if I want 10,11,12,13,etc to = 100?

    Is there a way to do this by using something like this?

    if (numbers.equals("9"++))
    

    or is there some kind of wildcard in android like

    if (numbers.equals("1"+"*"))
    

    i know if i replace the * with zero it will be 10 so if this is possible I could make one for 1, one for 2, one for 3, etc. and this would still save me so much code.
    If this is not possible let me know if you have any ideas.

    Thanks guys!