My try catch function won't work as expected

家住魔仙堡 提交于 2020-01-08 07:40:30

问题


My program is supposed to take the given value and find the other value with it. However, my try catch statement doesn't seem to be working. It never does the first 2 if statements which are the most essential parts of the program. The 3rd one does work when you input both values. Thanks in Advance.

        public void calculate(View view) {
    EditText length_find = findViewById(R.id.feet);
    EditText pounds_find = findViewById(R.id.pounds);

    try {
        //int length_int = Integer.parseInt(length_find.getText().toString());
        //int pounds_int = Integer.parseInt(pounds_find.getText().toString());

        double d = .29;
        //double length = length_int;
        //double pounds = pounds_int;
        double w = 24.5 / 12;
        double h = .002 / 12;

        //This Calculates if Pounds are given
        if ((length_find).getText().toString().trim().length() == 0){
            Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
            int pounds_int = Integer.parseInt(pounds_find.getText().toString());
            double pounds = pounds_int;
            double v = pounds / d;
            double length = v / w / h;
            final TextView mTextView = (TextView) findViewById(R.id.length_show);
            mTextView.setText((int) length);
        }


        //This Calculates if Length is given
        if ((pounds_find).getText().toString().trim().length() == 0){
          Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
          int lenght_int = Integer.parseInt(length_find.getText().toString());
          double length = lenght_int;
          double v = length * w * h;
          double answer_pounds = v * d;
          final TextView mTextView = (TextView) findViewById(R.id.pound_show);
          mTextView.setText((int) answer_pounds);
        }
        if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
            Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
        }

    }
    catch(Exception ex) {
            Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
        }

    }

回答1:


As i understood,

(pounds_find).getText().toString().trim().length() <= 0

or

(length_find).getText().toString().trim().length() <= 0

is true only when user hasn't input anything. if he has, then 3rd "if" execute as expected. But when user hasn't input anything then

int length_int = Integer.parseInt(length_find.getText().toString());
int pounds_int = Integer.parseInt(pounds_find.getText().toString());

cannot be done. because it is empty. I think that's the case here. If that isn't please let me know. If i'm wrong sorry. That's what i understood from here. If you could debug and be specific about scenario or exception if it throw any, that would be more helpful



来源:https://stackoverflow.com/questions/53967597/my-try-catch-function-wont-work-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!