Android中的NumberFormat异常

时间:2015-10-01 03:44:20

标签: android

这是我的计算器应用程序,我知道当我们尝试将字符串转换为数字类型时会引起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();
    }
}

2 个答案:

答案 0 :(得分: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
    }

希望这个帮助

答案 1 :(得分:0)

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

希望它有所帮助。