0

我制作了一个计算几何平均值的程序,我的程序已经在运行,但我有一些错误。每当我在 inputValues 中单击带有空输入的 btnCalculate 时,我的程序就会停止工作。我将如何处理这个错误?谢谢:)

    final AutoCompleteTextView inputValues = (AutoCompleteTextView) findViewById(R.id.txt_input);
    final TextView txtTotalNum = (TextView) findViewById(R.id.txt_totalNumber);
    final TextView GeoMean= (TextView) findViewById(R.id.txt_GeoMean);

    Button btnCalculate = (Button) findViewById(R.id.btncalculate);
    btnCalculate.setOnClickListener(new OnClickListener(){


        @Override
        public void onClick(View agr0) {
            String []values = ( inputValues.getText().toString().split(","));//this is the inputed values in the editText using split method
            txtTotalNum.setText(Integer.toString(values.length));//calculate the number of inputs
            double[] convertedValues = new double[values.length];

            double product1 =1.0;
            double product=1.0;

            for(int a = 0; a < convertedValues.length; a++){
               convertedValues[a] =Integer.parseInt(values[a]);
               //product *=convertedValues[a];
            }

            double geoMean = Math.pow(product, product1/convertedValues.length);
            GeoMean.setText(Double.toString(geoMean));

        }

    });

    Button btnclear = (Button)findViewById(R.id.btnclear);
    btnclear.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
         inputValues.setText("");
         txtTotalNum.setText("");
         GeoMean.setText("");


        }
4

4 回答 4

0
        @Override
        public void onClick(View agr0) {
            String []values = ( inputValues.getText().toString().split(","));//this is the inputed values in the editText using split method
if(values !=NULL){
            txtTotalNum.setText(Integer.toString(values.length));//calculate the number of inputs
            double[] convertedValues = new double[values.length];

            double product1 =1.0;
            double product=1.0;

            for(int a = 0; a < convertedValues.length; a++){
               convertedValues[a] =Integer.parseInt(values[a]);
               //product *=convertedValues[a];
            }

            double geoMean = Math.pow(product, product1/convertedValues.length);
            GeoMean.setText(Double.toString(geoMean));
}
        }
于 2013-09-18T07:19:57.293 回答
0
// try this
btnCalculate.setOnClickListener(new OnClickListener(){


        @Override
        public void onClick(View agr0) {
            if(inputValues.getText().toString().length()>0){
            String []values = ( inputValues.getText().toString().split(","));//this is the inputed values in the editText using split method
            txtTotalNum.setText(Integer.toString(values.length));//calculate the number of inputs
            double[] convertedValues = new double[values.length];

            double product1 =1.0;
            double product=1.0;

            for(int a = 0; a < convertedValues.length; a++){
               convertedValues[a] =Integer.parseInt(values[a]);
               //product *=convertedValues[a];
            }

            double geoMean = Math.pow(product, product1/convertedValues.length);
            GeoMean.setText(Double.toString(geoMean));
          }

        }

    });
于 2013-09-18T07:20:19.650 回答
0

你的程序停在

 convertedValues[a] =Integer.parseInt(values[a]);

因为对于空输入,拆分数组没有值,所以它的长度为 0。当您尝试访问时,values[0]您会得到一个 ArrayIndexOutOfBoundsException。

只需在拆分之前检查输入是否为空。

if(!inputValues.getText().toString().equals("")){
    ... // do your stuff
} else {
  // show a message
}
于 2013-09-18T07:21:25.570 回答
0

您收到错误是因为您的代码试图拆分空字符串或 null,然后访问实际上根本没有索引的 values[]。只需检查 EditText 获取的字符串的长度是否>0。

@Override
    public void onClick(View agr0) {
        String fetchedValues = ( inputValues.getText().toString());
        if(fetchedValues.length>0)
        {
          String []values = fetchedValues.split(",");
          txtTotalNum.setText(Integer.toString(values.length));//calculate the number of inputs
         double[] convertedValues = new double[values.length];

         double product1 =1.0;
         double product=1.0;

         for(int a = 0; a < convertedValues.length; a++){
            convertedValues[a] =Integer.parseInt(values[a]);
            //product *=convertedValues[a];
         }

         double geoMean = Math.pow(product, product1/convertedValues.length);
          GeoMean.setText(Double.toString(geoMean));
       }
       else
       {
          //alert the user that the field is empty
        }
    }
于 2013-09-18T07:26:24.447 回答