Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android integer.parseInt()不';不适用于一位数整数_Java_Android_Parseint - Fatal编程技术网

Java Android integer.parseInt()不';不适用于一位数整数

Java Android integer.parseInt()不';不适用于一位数整数,java,android,parseint,Java,Android,Parseint,我使用的是AndroidEclipseIndigo版本2,但parseInt()函数有问题。我有一个字符串缓冲区,我正在解析它以获得一个整数值。问题是,它只适用于2位整数,而不适用于单个数字。我进一步尝试使用“基数”子句来纠正这个问题,但没有成功。如果我用基数硬编码字符串,它会工作: case PIN_STATE: int pin_state = 0; String statebuff = (String) msg.obj

我使用的是AndroidEclipseIndigo版本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.

int v=Integer.parseInt(statebuff.trim());//隐马尔可夫模型。。。试试
.valueOf
“不适用于单位数整数”:你真的认为像这样的错误到目前为止还没有被发现吗?我是新来的、未经调查的亨利。但是要回答你的问题-不。当一个人不经常在这个空间中编程时,他/她没有意识到许多基本的事情。这里有一个是给你们的:圣经上说,根据圣经,耶稣“又复活了”。很多人都应该问但还没有问的64k问题是:“他第一次或上一次从坟墓里复活是什么时候?”除非我们在生活中的某些范例中努力工作/学习,否则我们往往会意识到显而易见的(隐藏在我们鼻子底下)你也可以使用Integer.parseInt(statebuff)。默认情况下,基数是10。我用您的更改对它进行了测试,它似乎解决了这个问题。这很有道理。但令人惊讶的是,当程序试图解析包含空格的字符串值时,NumberFormatException没有抛出。谢谢你,艾略特。