我正在创建一个应用程序,用户可以在一些活动中输入7-10个数字值。然后在相当分层的方程式中使用条目,该方程式将结果返回给用户。两个值给我带来麻烦,并产生了我无法弄清的NumberFormatException
。
从微调器中选择一个条目IHCValue
,因为只有4种可能的选择:
<string-array name="ihc_choices">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
此选择不仅可用于公式中,还可确定是否需要其他信息。
获得IHC值:
private void directSpinner(){
int spinnerPosition = scoreSelect.getSelectedItemPosition();
String[] IHC_Values = getResources().getStringArray(R.array.ihc_choices);
String IHCValue = String.valueOf(IHC_Values[spinnerPosition]);
SharedPrefManager.write(IHC_VALUE, IHCValue);
//The string to manage Intent defined separately as the intent did not recognize the value of 'String IHCValue'
String IHCEntry = scoreSelect.getSelectedItem().toString();
if (IHCEntry.equals("2")){
//Status equivocal, must collect more information before calculation can be performed.
Intent next = new Intent(getApplicationContext(), FishScoreActivity.class);
startActivity(next);
} else { //IHCValue of 0,1,3
//Status is defined, we have enough info to perform calculation.
Intent last = new Intent(getApplicationContext(), CancerDemographicActivity.class);
startActivity(last);
}
}
如果用户从该下拉列表中选择“ 2”,则下一个活动会询问FISHEntry
,也用于等式的一部分:
获得FISH条目:
public boolean validateData(){
boolean validData = false;
String FISHEntry = enterFISH.getText().toString();
if (!FISHEntry.equals("")){
SharedPrefManager.write(FISH_RATIO, FISHEntry);
validData = true;
} else {
Toast.makeText(getApplicationContext(), "Please enter FISH ratio from your report.", Toast.LENGTH_LONG).show();
validData = false;
}
return validData;
}
然后,用户可以按按钮并运行方程式。如果用户选择的IHCValue
为0,1或3,则不需要FISHEntry
并且方程运行良好。
如果(且仅当)IHCValue = 2
,则必须在Equation类中执行附加计算,并使应用程序崩溃。
我在应用程序中执行的步骤:
IHCValue
。FISHEntry = 2.3
然后应用程序崩溃:
Caused by: java.lang.NumberFormatException: Invalid int: "2.3"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at com.them.health.Equations.MasterEquation.<clinit>
(MasterEquation.java:42)
“ 2.3”是我的FISHEntry
,而第42行声明了IHCValue
:
MasterEquation.java:42
private static int mIHCValue = Integer.parseInt(SharedPrefManager.read(IHC_VALUE, null));
FISHEntry在MasterEquation.java:41上声明:
private static double mFISHRatio = Double.valueOf(SharedPrefManager.read(FISH_RATIO, null));
为保持一致,在执行方程式时,所有值都以字符串形式保存到SharedPrefs中,并根据需要解析int或double。另外,将IHCValue
直接保存为整数时,我收到了字符串到整数的错误。
方法,该方法使用以下变量:
private void calcRatioOne(){
double innerRatio = mIHCValue/mFISHRatio;
if (mIHCValue == 0 || mIHCValue == 1){
mRatioOne = 0.0;
} else if (mIHCValue == 3){
mRatioOne = 12.75525;
} else if (mIHCValue == 2){ //IHC = 2 takes FISHEntry into account.
if (innerRatio >= 2.2){
mRatioOne = 12.75525;
}
if (innerRatio >= 1.8){
mRatioOne = 1.46495;
}
if (innerRatio < 1.8){
mRatioOne = 0.0;
}
}
}
private double mRatioOne;
是在开头定义的。
我尝试了以下操作:
innerRatio
计算的方法。 同样的问题。 Integer
,而不是int
。 同样的问题。 仅当IHCValue = 2
时这才是问题如果有帮助,我不反对将IHC条目从微调器更改为另一种输入方法,但我希望避免使用自由文本,因为这是序列中最重要的条目。
我的SharedPrefManager:
已声明密钥:
public static final String IHC_VALUE = null;
读写方法:
public static String read(String key, String defValue){
return userPrefs.getString(key, defValue);
}
public static void write(String key, String value) {
SharedPreferences.Editor prefsEditor = userPrefs.edit();
prefsEditor.putString(key, value);
prefsEditor.commit();
}
答案 0 :(得分:2)
从错误消息中可以明显看出问题出在哪里。您试图将值“ 2.3”分配给 integer-type 变量。
最好的解决方案是使用大多数工具中提供的调试器工具 集成开发环境(IDE),例如IntelliJ或 Eclipse。使用该调试器工具,找出错误值的原因 首先分配给
IHCValue
,然后采取相应措施进行修复 错误。
我不同意@shark的评论中提出的解决方案;将Float
分配给int
是不兼容的操作。我的建议是将 mIHCValue 的类型更改为double
。
如果 mIHCValue 必须为整数,则将其从字符串转换为双精度,然后转换为整数。像这样:
private double originalIHC = Double.valueOf(SharedPrefManager.read(IHC_VALUE, null));
private static int mIHCValue = (int) originalIHC;
让我解释一下为什么要两行这样做。由于String的值不是整数,而是浮点数,因此我们必须将其解析为double first。但是无法将String转换为原始double
,因此我们必须为此使用Wrapper类Double
。
包装器类Double
(装箱)与double
以外的其他基本类型不兼容,因此我们首先将“包装”值分配给其基本类型。
将初始值转换为双基元后,我们可以将其转换为我显示的整数。
注意,该解决方案将帮助您仅获取任何浮点数的整数部分。例如,如果 mIHCValue
为2.8
或2.0012
或2.532
或2.9999999
,则结果整数将始终为 2
。因此,请谨慎考虑要达到的目标。
希望这会有所帮助。