【问题标题】:NumberFormat Exception in Android [duplicate]Android中的NumberFormatException [重复]
【发布时间】:2015-10-01 12:52:51
【问题描述】:

这是我的计算器应用程序,我知道 NumberFormat 是在我们尝试将字符串转换为数字类型时引起的。我也用 TRY/CATCH 包围了它们,但我似乎无法将它们作为 INT 值。在我的应用程序中,我在 textview 中获取字符串并尝试对它们执行操作。

谁能提出解决问题的替代方法?

代码如下:

public class MainActivity extends AppCompatActivity {

    private static String TAG = MainActivity.class.getSimpleName();
    private static String GAT = "Tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    ArrayList<String> arrayList = new ArrayList<String>();

    String stringOne = " ";
    String stringTwo = " ";

    public void onClick1(View view) {
        //Getting Input from TextView
        TextView inputText = (TextView) findViewById(R.id.inputTextView);
        Button button = (Button) view;

        //Store as String from button press
        stringOne = (String) button.getText().toString();
        Log.d(TAG, stringOne);
        //For entering multiple values
        if (!stringOne.contains("+") && !stringOne.contains("-") && !stringOne.contains("/") && !stringOne.contains("*")) {
            //Concat if it has multiple digits to original string
            stringTwo = stringTwo + stringOne;
            Log.d(TAG, stringTwo);
            //Remove the last string and place as StringTwo
            if (arrayList.size() > 0) {
                //Get last position in the array
                arrayList.remove((arrayList.size() - 1));

            }
            arrayList.add(stringTwo);
        } else {
            //For operators add two times because we removed the previous index
            arrayList.add(stringOne);
            arrayList.add(stringOne);
            //Clear
            stringTwo = " ";

            Toast.makeText(this, stringOne, Toast.LENGTH_LONG).show();
            Log.d(TAG, stringOne);

            // Log.d("Veer",stringTwo);


        }

        //Add to TextView
        inputText.setText(inputText.getText().toString()+stringOne);
        //inputText.setText(arrayList.toString());
    }


    public void calculate(View view) {
        TextView outputText = (TextView) findViewById(R.id.outputTextView);
        int result = 0;
        int list = arrayList.size();

        while (list != 1) {
            if (list > 3) {
                //Considering the equation to be like 4+5*5-2/4, Get the third operator, if * and /, then multiply
                if (arrayList.get(3).contains("*") || arrayList.get(3).contains("/")) {
                    if (arrayList.get(3).contains("*")) {
                        result = Integer.parseInt(arrayList.get(2)) * Integer.parseInt(arrayList.get(4));
                    } else if (arrayList.get(3).contains("/")) {

                        result = Integer.parseInt(arrayList.get(2)) / Integer.parseInt(arrayList.get(4));
                    }
                    arrayList.remove(2);
                    arrayList.remove(2);
                    arrayList.remove(2);
                    arrayList.add(2, Integer.toString(result));
                    list = arrayList.size();
                } else {
                    //Vice versa, here for + and - ,replace 1st and 2nd digit
                    if (arrayList.get(1).contains("+")) {

                        result = Integer.parseInt(arrayList.get(0)) + Integer.parseInt(arrayList.get(2));
                    }
                    if (arrayList.get(1).contains("-")) {

                        result = Integer.parseInt(arrayList.get(0)) - Integer.parseInt(arrayList.get(2));
                    }
                    if (arrayList.get(1).contains("*")) {

                        result = Integer.parseInt(arrayList.get(0)) * Integer.parseInt(arrayList.get(2));
                    }
                    if (arrayList.get(1).contains("/")) {

                        result = Integer.parseInt(arrayList.get(0)) / Integer.parseInt(arrayList.get(2));
                    }
                    arrayList.remove(0);
                    arrayList.remove(0);
                    arrayList.remove(0);
                    arrayList.add(0, Integer.toString(result));
                    list = arrayList.size();
                }
            }
            else
            {
                //If size is 3
                if (arrayList.get(1).contains("+")) {

                    result = Integer.parseInt(arrayList.get(0)) + Integer.parseInt(arrayList.get(2));
                }
                if (arrayList.get(1).contains("-")) {

                    result = Integer.parseInt(arrayList.get(0)) - Integer.parseInt(arrayList.get(2));
                }
                if (arrayList.get(1).contains("*")) {

                    result = Integer.parseInt(arrayList.get(0)) * Integer.parseInt(arrayList.get(2));
                }
                if (arrayList.get(1).contains("/")) {

                    result = Integer.parseInt(arrayList.get(0)) / Integer.parseInt(arrayList.get(2));
                }
                arrayList.remove(0);
                arrayList.remove(0);
                arrayList.remove(0);
                arrayList.add(0, Integer.toString(result));
                list = arrayList.size();
            }
        }
        outputText.setText(Integer.toString(result));
    }


    public void clearView(View view) {
        TextView input = (TextView)findViewById(R.id.inputTextView);
        TextView output = (TextView)findViewById(R.id.outputTextView);
        stringOne = "";
        stringTwo = "";
        input.setText("");
        output.setText("");
        arrayList.clear();
    }
}

【问题讨论】:

  • 什么时候出错?为什么你点尝试 double 而不是 int ?
  • 我收到运行时错误,具体取决于方程式,如果它是 2*3,那么我在 if (arrayList.get(1).contains("*")) { result = Integer 上收到错误.parseInt(arrayList.get(0)) * Integer.parseInt(arrayList.get(2));

标签: android


【解决方案1】:

将字符串转换为数字类型时,您需要捕获异常。如果它抛出异常,则返回。如果没有例外,您继续对它们执行操作。

    String text = "";
    int num;
    try {
       num = Integer.parseInt(text);
       // text is a number");
    } catch (NumberFormatException e) {
       Toast.makeText(MainActivity.this, "Can not parse string to int: " + text,Toast.LENGTH_LONG).show();

       // text is not a number";
       // Show Log or make a Toast here to easy see when String is not Int format. After that find the reason why text is not int format
    }

希望有帮助

【讨论】:

  • 好的,现在不会崩溃,但是我如何将它们作为 INT
  • 当然,如果你输入的STRING数据是正确的,你可以解析成INT。因此,如果您无法获得 INT 的值,则您的 STRING 不是 INT 格式
  • 你输入的字符串数据是什么意思?
  • 如果你的字符串输入是“2”,你可以将它解析为int,但输入字符串是“2”或“a”或“2”,你会得到错误
  • 我想我的输入是正确的,你能验证一下吗?
【解决方案2】:
 float num1 = 0;
 float num2 = 0;
 float result = 0;

 num1 = Float.parseFloat(etNum1.getText().toString());
 num2 = Float.parseFloat(etNum2.getText().toString());

result = num1 * num2 ;
Log.e("Result",""+result);

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-11
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2011-05-23
    相关资源
    最近更新 更多