Daniel
Daniel

Reputation: 502

How can you compare strings in android with greater than

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!

Upvotes: 2

Views: 17270

Answers (2)

Dan Dyer
Dan Dyer

Reputation: 54495

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.

Upvotes: 3

Falmarri
Falmarri

Reputation: 48577

Is there a reason you can't use

Integer.valueOf("9");

Upvotes: 2

Related Questions