如何检查NumberFormatException:空字符串

时间:2018-12-26 21:29:43

标签: java android android-edittext numberformatexception

我想计算平均成绩,现在我有了基本知识和布局。将EditText保持打开状态时,它将引发NumberFormatException。有人可以解释一下如何检查吗?

欢迎任何提示等!

P.S。我的EditText设置为NumberDecimal,所以我没有“错误输入”类型。

final TextView averageView = findViewById(R.id.averageView); 
final String averageText = getString(R.string.average); 
final Button calculateButton = findViewById(R.id.calculateAverageButton); 
final Button addGradeButton = findViewById(R.id.addGradeButton)

calaculateButton.setOnClickListener(new View.onClickListener() { 
    @SupressLint("SetTextI18n")                                                     
    public void onClick(View v) { 

        double grade[]  = {Double.parseDouble(((EditText) findViewById(R.id.grade1)).getText().toString()); 

        double weight[] = {Double.parseDouble(((EditText) findViewById(R.id.weight1)).getText().toString()); 

        double weightTotal = weight[0]; double sum = grade[0] * weight[0] 

        double average = sum / weightTotal 

        @SuppressLint("DefaultLocale") String averageResult = String.format(%.2f, average);

        averageView.setText(averageText + " " + averageResult);

3 个答案:

答案 0 :(得分:1)

在尝试做会导致错误的事情之前,检查您的错误情况

final TextView averageView = findViewById(R.id.averageView); 
final String averageText = getString(R.string.average); 
final Button calculateButton = findViewById(R.id.calculateAverageButton); 
final Button addGradeButton = findViewById(R.id.addGradeButton)

EditText[] grades = new EditText[] {
    (EditText) findViewById(R.id.grade1)
};
EditText[] weights = new EditText[] {
    (EditText) findViewById(R.id.weight1)
};

calculateButton.setOnClickListener(new View.onClickListener() {   
    @Override                                               
    public void onClick(View v) { 
        String grade1 = grades[0].getText().toString();
        String weight1 = weights[0].getText().toString();

        if (TextUtils.isEmpty(grade1) || TextUtils.isEmpty(weight1)) {
            return; // TODO: Show some error
        }

        double sum = Double.parseDouble(grade1) * Double.parseDouble(weight1); 

答案 1 :(得分:1)

您可以尝试通过“ try-catch”来管理此类异常。

try {
   double grade = Double.parseDouble(((EditText) findViewById(R.id.numberanswer)).getText().toString());
   answer.setText("" + grade);
} catch (NumberFormatException e){
   Toast.makeText(getApplicationContext(),"Please insert a number",Toast.LENGTH_SHORT).show();
}

答案 2 :(得分:1)

我已经用“ try-catch”语句完成了该操作,每次打开一个字段时都会捕获NumberFormatException。

final TextView averageView = findViewById(R.id.averageView); 
final String averageText = getString(R.string.average); 
final Button calculateButton = findViewById(R.id.calculateAverageButton); 
final Button addGradeButton = findViewById(R.id.addGradeButton)

calaculateButton.setOnClickListener(new View.onClickListener() { 
    @SupressLint("SetTextI18n")                                                     
    public void onClick(View v) { 

       try {
        double grade[]  = {Double.parseDouble(((EditText) findViewById(R.id.grade1)).getText().toString()); 

        double weight[] = {Double.parseDouble(((EditText) findViewById(R.id.weight1)).getText().toString()); 

        double weightTotal = weight[0]; double sum = grade[0] * weight[0] 

        double average = sum / weightTotal 

        @SuppressLint("DefaultLocale") String averageResult = String.format(%.2f, average);

        averageView.setText(averageText + " " + averageResult);
      { catch (NumberFormatException e) {
          Toast.makeText(MainActivity.this, "There is a empty field!", Toast.LENGTH_LONG).show();
      }