Java 等式程序-错误似乎指向错误的行

Java 等式程序-错误似乎指向错误的行,java,android,Java,Android,我正在创建一个应用程序,用户在几个活动中输入7-10个数值。然后在一个相当分层的等式中使用条目,该等式将结果返回给用户。两个值给我带来了麻烦,并产生了我无法理解的NumberFormatException 从微调器中选择一个条目,IHCValue,因为只有4种可能的选择: <string-array name="ihc_choices"> <item>0</item> <item>1</item> <ite

我正在创建一个应用程序,用户在几个活动中输入7-10个数值。然后在一个相当分层的等式中使用条目,该等式将结果返回给用户。两个值给我带来了麻烦,并产生了我无法理解的
NumberFormatException

从微调器中选择一个条目,
IHCValue
,因为只有4种可能的选择:

<string-array name="ihc_choices">
    <item>0</item>
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>
如果用户从此下拉列表中选择“2”,则下一个活动要求输入
FISHEntry
,也用于部分等式:

获取鱼条目:

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

        }
    }
}
然后,用户可以按下一个按钮并运行方程式。如果用户选择的
IHCValue
为0,1或3,则不需要
FISHEntry
,且方程运行良好

如果(且仅当)
IHCValue=2
则必须在Equation类中执行额外的计算,并使应用程序崩溃

我在应用程序中执行的步骤:

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

        }
    }
}
  • 从微调器中,选择2的
    IHCValue
  • 提示输入FISH分数,
    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)
    
    FISHEntry在MasterEquation上声明。java:41:

    private static double mFISHRatio = Double.valueOf(SharedPrefManager.read(FISH_RATIO, null));
    
    为了保持一致,所有值都以字符串的形式保存到SharedPref中,在执行表达式时,我会根据需要解析int或double。另外,在将
    IHCValue
    直接保存为int时,我收到了一个字符串到整数的错误

    MasterEquation.java中使用变量的方法:

       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);
            }
        }
    
    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;
    }
    
    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;
    
            }
        }
    }
    
    读写方法:

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

    从错误消息中可以明显看出问题所在。您正在尝试将值“2.3”分配给整型变量

    最好的解决方案是使用most中提供的调试器工具 集成开发环境(IDE),如或 . 使用该调试器工具,找出错误值的原因 首先分配给
    IHCValue
    ,并相应地进行修复 错误

    我不同意@shark在评论中提出的修复方案;将
    Float
    分配给
    int
    是不兼容的操作。我的建议是将mIHCValue的类型更改为
    double

    如果mIHCValue必须是整数,则将其从字符串转换为double,然后转换为整数。像这样:

    private double originalIHC = Double.valueOf(SharedPrefManager.read(IHC_VALUE, null));
    private static int mIHCValue = (int) originalIHC;
    
    让我用两行文字来解释我为什么这么做。由于字符串的值不是整数,而是浮点数,因此我们必须首先将其解析为双精度。但是将字符串转换为原语
    double
    是不可能的,因此我们必须使用包装类
    double

    包装类
    Double
    (装箱)与
    Double
    以外的基元类型不兼容,因此我们首先将“wrapped”值分配给它的基元

    在将初始值转换为double原语后,我们可以将其转换为如我所示的整数

    注意此解决方案将帮助您仅获取任何浮点数的整数部分。例如,如果
    mIHCValue
    2.8
    2.0012
    2.532
    2.999999
    ,则结果整数将始终为
    2
    。所以,要小心,想想你想要实现什么


    希望这有帮助。

    从错误消息中可以明显看出问题所在。您正在尝试将值“2.3”分配给整型变量

    最好的解决方案是使用most中提供的调试器工具 集成开发环境(IDE),如或 . 使用该调试器工具,找出错误值的原因 首先分配给
    IHCValue
    ,并相应地进行修复 错误

    我不同意@shark在评论中提出的修复方案;将
    Float
    分配给
    int
    是不兼容的操作。我的建议是将mIHCValue的类型更改为
    double

    如果mIHCValue必须是整数,则将其从字符串转换为double,然后转换为整数。像这样:

    private double originalIHC = Double.valueOf(SharedPrefManager.read(IHC_VALUE, null));
    private static int mIHCValue = (int) originalIHC;
    
    让我用两行文字来解释我为什么这么做。由于字符串的值不是整数,而是浮点数,因此我们必须首先将其解析为双精度。但是将字符串转换为原语
    double
    是不可能的,因此我们必须使用包装类
    double

    包装类
    Double
    (装箱)与
    Double
    以外的基元类型不兼容,因此我们首先将“wrapped”值分配给它的基元

    在将初始值转换为double原语后,我们可以将其转换为如我所示的整数

    注意此解决方案将帮助您仅获取任何浮点数的整数部分。例如,如果
    mIHCValue
    2.8
    2.0012
    2.532
    2.999999
    ,则结果整数将始终为
    2
    。所以,要小心,想想你想要实现什么


    希望这能有所帮助。

    您是否尝试过使用调试器单步通过它?尽管这很简单:
    原因:java.lang.NumberFormatException:无效int:“2.3”
    请change@Shark我感谢你的答复。一开始,我不确定从哪里开始这个问题——没有调试经验也没有帮助。我整个上午都在探索调试器。哇!令人惊讶的帮助和洞察力。我可以告诉你,这已经减少了我需要在这里和向我的朋友提问的数量。@Shark我正在窃取你的报价:)!这里只是增加了一点:使用IDE编写代码。总是。它使发现错误和开发高质量代码变得更加容易。我个人最喜欢的,也是普遍流行的IDE是。有一个免费的版本,但如果你是一个学生