如何解决如何比较字符串或文本框值大于 x 但小于 y在 2 个值之间
我的应用程序正在将 inputStream 写入字符串,然后将字符串写入 textView。 在此之下,我使用 if 语句控制我的 UI 动画。 现在我不知道如何进行 if 语句: if (BTtext => 100 && BTtext
从 100 到 350 的值用于调整搜索栏位置。在这种情况下,我还有其他需要忽略的值。 我试过:
if(strInput > lowervol && strInput < uppervol) {
}
strInput 是我的 inputStream。 lowervol 和 uppervol 是这样创建的:
final static String lowervol = "99";
final static String uppervol = "351";
如果我这样做,我会得到:
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
这是怎么做到的? 谢谢
解决方法
您无法将实际字符串与 >
或 <
进行比较。在您的情况下,您实际想要比较的是字符串中包含的数字,而不是字符串本身。
为此,您的常量可能应该存储为 int
:
static final int LOWER_VOL= 99;
static final int UPPER_VOL= 351;
在比较之前,您的 strInput
也应转换为 int
。像这样:
int inputValue = Integer.parseInt(strInput); // parsing exceptions to be handled here
if(inputValue > lowervol && inputValue < uppervol) {
// do stuff
}
当从上面调整常量的命名约定时,最终的代码片段将如下所示:
int inputValue = Integer.parseInt(strInput); // parsing exceptions to be handled here
if(inputValue > LOWER_VOL && inputValue < UPPER_VOL) {
// do stuff
}
,
你必须尝试整数值。请在下面尝试。
Integer.parseInt(string);
,
谢谢,我刚刚解决了。
int potVal=Integer.parseInt(strInput);
if(potVal >= 100 && potVal <= 350) {
volumeSlider = (SeekBar) findViewById(R.id.volumeSlider);
volumeSlider.setProgress(potVal);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。