Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

java - How to compare string or textbox value (is bigger than x but smaller than y) (in between of the 2 values)

My app is writing inputStream to string, then string to textView. Under that I controll my UI animations with if statements. Now I don`t know how to make if statement: if (BTtext => 100 && BTtext <=350)

Values from 100 to 350 are for seekbarposition adjustment. I also have other values that need to be ignored in that case. I had tryed:

if(strInput > lowervol && strInput < uppervol) {
}

strInput is my inputStream. lowervol & uppervol are created as so:

    final static String lowervol = "99";
    final static String uppervol = "351";

If I do it like that I get:

error: bad operand types for binary operator '>'
first type:  String
second type: String

error: bad operand types for binary operator '<'
first type:  String
second type: String

How can this be done? Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't compare actual strings with > or <. What you actually want to compare in your case is the number contained in the string, not the string itself.

For that, your constants should probably be stored as int:

static final int LOWER_VOL= 99;
static final int UPPER_VOL= 351;

And before comparison, your strInput should be converted to int aswell. Something like this:

int inputValue = Integer.parseInt(strInput); // parsing exceptions to be handled here
if(inputValue > lowervol && inputValue < uppervol) {
    // do stuff
}

When adapting the naming convention of the constants from above, the final snippet will look like this:

int inputValue = Integer.parseInt(strInput); // parsing exceptions to be handled here
if(inputValue > LOWER_VOL && inputValue < UPPER_VOL) {
    // do stuff
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...