How can you compare strings in android with greater than

可紊 提交于 2019-12-19 11:06:32

问题


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!


回答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.




回答2:


Is there a reason you can't use

Integer.valueOf("9");


来源:https://stackoverflow.com/questions/4092161/how-can-you-compare-strings-in-android-with-greater-than

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!