我正在使用 Android Eclipse Indigo Release 2,但在使用 parseInt() 函数时遇到问题。我有一个字符串缓冲区,我正在解析它以获取整数值。问题是它只能正确地处理 2 位整数,而不是单个数字。我进一步尝试使用“基数”子句来纠正问题,但没有成功。如果我用基数对字符串进行硬编码,它就可以工作:
case PIN_STATE:
int pin_state = 0;
String statebuff = (String) msg.obj;
if (msg.arg1 > 0) {
try{
pin_state=Integer.parseInt(statebuff,10); //Doesn't work for single-digit integers
pin_state=Integer.parseInt("01",10); // equals = 1 hard code works below correctly
} catch(NumberFormatException nfe) {
return;
}
switch (pin_state){
case 1: //RESYNC_THERMO_ON: //Sync module state - thermostat is on
ThermoCheck.setChecked(true);
ThermoTxt.setText(R.string.ThermoOn);
ThermoTxt.setTextColor(Color.BLUE);
ThermoTxt.setVisibility(View.VISIBLE);
fanSpeedTxt.setVisibility(View.INVISIBLE);
break;
case 12: //work great without any modifications to the parseInt() function above
ThermoCheck.setChecked(true);
ThermoTxt.setText(R.string.ThermoOn);
ThermoTxt.setTextColor(Color.BLUE);
ThermoTxt.setVisibility(View.VISIBLE);
fanSpeedTxt.setVisibility(View.INVISIBLE);
break;
default: //Do something
}
}
最佳答案
你的说法是错误的,
String statebuff = "1";
int v = Integer.parseInt(statebuff,10);
System.out.println(v);
打印1.您的statebuff
值不是您想象的那样,我的假设是它有一个空格。
int v = Integer.parseInt(statebuff.trim()); // <-- add a trim call, also parseInt is
// decimal by default so 10 is
// redundant.
关于java - Android integer.parseInt() 不适用于单位数整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24657656/