【问题标题】:Error in converting height from centimeters to feet inches将高度从厘米转换为英尺英寸时出错
【发布时间】:2016-09-10 10:15:01
【问题描述】:

我编写了一个方法,通过单击切换按钮将高度从厘米转换为英尺英寸,但是当我在输入以厘米为单位的高度后尝试单击切换按钮时,它会抛出错误 java.lang.NumberFormatException:无效的 int:“” 这是我的代码:

private void convertTofeetInches(EditText height_cm){
    String str = height_cm.getText().toString();
     int feet = (int) Math.floor(Integer.parseInt(str)/30.48);
    int inches = (int)Math.round((Double.parseDouble(str)/2.54) - ((int)feet * 12));
    Log.d("feet",String.valueOf(feet));
    Log.d("inches",String.valueOf(inches));
    enter_height.setText(""+feet + "'" +inches + "\"");


}

我认为我在类型转换方面犯了一些错误。谁能指点一下。

【问题讨论】:

标签: android


【解决方案1】:

试试这个,

private void convertTofeetInches(String str) throws NumberFormatException{
    Double value = new Double(str);
    int feet = (int) Math.floor(value / 30.48);
    int inches = (int) Math.round((value / 2.54) - ((int) feet * 12));
    String ouput = feet + "' " + inches + "\"";
    enter_height.setText(ouput);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多