0

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

Kaja Alyo
  • 1
  • 1
  • 2
    You can't just compare strings with mathematical operators. You need to convert the strings into numbers (see https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – randmon Jan 12 '21 at 12:35
  • So you need to convert your input to a number (and handle any exceptions if somehow your input is not a number!!!) and you can just have lowervol and uppervol begin as integers (`final static int lowervol = 99;`) for example – randmon Jan 12 '21 at 12:36

3 Answers3

2

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
}
maloomeister
  • 2,461
  • 1
  • 12
  • 21
  • 1
    Just commenting that after renaming the `lowervol` and `uppervol` as constants (correctly so) the `if` statement should probably use the updated names (although I can see arguments both ways depending on if the first piece of advice was ignored for whatever reason). Otherwise good answer. – BeUndead Jan 12 '21 at 13:04
  • 1
    @BeUndead Good point, thanks for that. I updated the answer and added the final snippet which uses the renamed constants. – maloomeister Jan 12 '21 at 13:07
1

you must try integer value. Please try below.

Integer.parseInt(string);
Rest
  • 119
  • 8
0

Thank you, I had just got it solved.

                                int potVal=Integer.parseInt(strInput);
                            if(potVal >= 100 && potVal <= 350) {
                                volumeSlider = (SeekBar) findViewById(R.id.volumeSlider);
                                volumeSlider.setProgress(potVal);
                            }
Kaja Alyo
  • 1
  • 1