android - 将高度从厘米转换为英尺英寸时出错

标签 android

我编写了一种方法,通过单击切换按钮将高度从厘米转换为英尺英寸,但是当我在输入以厘米为单位的高度后尝试单击切换按钮时,它抛出了我的错误 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 + "\"");


}

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

最佳答案

试试这个,

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);
}

关于android - 将高度从厘米转换为英尺英寸时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39425026/

相关文章:

android - 如何检测多个图像目标?

android - 垂直 ScrollView 内的 ViewPager 中的选项卡不会垂直滚动

Android:如何等待 AsyncTask 在 MainThread 中完成?

android - 覆盖android中的挂起转换

java - 如何处理fcm通知数据而不需要点击android中的通知弹出窗口

android - 请安装包 : 'Android Support Library'

android - 为什么unity OnApplicationPause在android上被调用两次?

更新应用程序时的 Android 内部存储

java - 键盘滑出/配置更改后 Android 循环速度更快

java - 如何通过忽略大小写来过滤 Recycleview?