Isaías
Isaías

Reputation: 491

Empty EditText error (Lots of EditTexts)

The activity contains a lot of EditTexts (about 40) where the user enters numbers, and then they're added. But when one of these EditTexts is empty, it gives an error and the app stops. I've read lots of solutions, but I don't want to write code for each EditText.

Here is some code (Wrote 450 lines, don't gonna show them all, but all of them are similar)

EditText mark1;
EditText mark2;
EditText mark3;
EditText mark4;
EditText mark5;
TextView marksem1;
Button calcsem1;
double mark1calc=0;
double mark2calc=0;
double mark3calc=0;
double mark4calc=0;
double mark5calc=0;
double totalsem1=0;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.etsia_gia_ata_layout);
    initControls();

private void initControls() {

    // Course 1 sem 1 calc
    mark1=(EditText)findViewById(R.id.mark1);
    mark2=(EditText)findViewById(R.id.mark2);
    mark3=(EditText)findViewById(R.id.mark3);
    mark4=(EditText)findViewById(R.id.mark4);
    mark5=(EditText)findViewById(R.id.mark5);
    marksem1=(TextView)findViewById(R.id.total1calc);
    calcsem1=(Button)findViewById(R.id.total1);
    calcsem1.setOnClickListener(new Button.OnClickListener()
      {public void onClick
      (View  v) {calcsem1();}

    private void calcsem1() {
        mark1calc=Double.parseDouble(mark1.getText().toString());
        mark2calc=Double.parseDouble(mark2.getText().toString());
        mark3calc=Double.parseDouble(mark3.getText().toString());
        mark4calc=Double.parseDouble(mark4.getText().toString());
        mark5calc=Double.parseDouble(mark5.getText().toString());
        totalsem1=(9*mark1calc+6*mark2calc+6*mark3calc+6*mark4calc+3*mark5calc)/30;
        marksem1.setText(Double.toString(totalsem1));
        marksem1.setText(String.format("%.2f", totalsem1));
    }});
    }
}

Hope there's a solution. Thank you very much!

Upvotes: 1

Views: 147

Answers (4)

Prasad.CH
Prasad.CH

Reputation: 492

you can replace your parsing statement with terinary statement as below:

mark2calc = mark4.getText().toString().equals("") ? 0 : Double.parseDouble(mark4.getText().toString());

Upvotes: 0

Eran Goldin
Eran Goldin

Reputation: 969

I assume your problem is with parseDouble. You can write your own protected parseDouble, which will try-catch to parse the text, and if an exception is thrown, will just return zero.

Something like this:

private static double safeParseDouble(String text)
{
    try 
    { 
        double result = Double.parseDouble(text);
        return result;
    }
    catch (Exception ex)
    {
        return 0;
    }
}

Upvotes: 1

Mark Ormesher
Mark Ormesher

Reputation: 2408

Your problem comes from the fact that Double.parseDouble(...) on an empty string will throw a NumberFormatException. You could replace each line with somthing like this:

try {
    mark1calc = Double.parseDouble(mark1.getText().toString());
} catch (NumberFormatException e) {
    mark1calc = 0;
}

This is a lot of extra code, so I'd suggest implementing something like this:

public static Double safeParse(String input) {
    try {
        return Double.parseDouble(input);
    } catch (NumberFormatException e) {
        return 0d;
    }
}

// ...

mark1calc = safeParse(mark1.getText().toString());
mark2calc = safeParse(mark2.getText().toString());
mark3calc = safeParse(mark3.getText().toString());
// ...

(This assumes you want an empty box to be worth 0; you can adjust the default value to your needs)

Upvotes: 1

asylume
asylume

Reputation: 544

Double.parseDouble() can throw exceptions so you will need to use try-catch block.

try {
    mark1calc=Double.parseDouble(mark1.getText().toString());
    mark2calc=Double.parseDouble(mark2.getText().toString());
    mark3calc=Double.parseDouble(mark3.getText().toString());
    mark4calc=Double.parseDouble(mark4.getText().toString());
    mark5calc=Double.parseDouble(mark5.getText().toString());
    totalsem1=(9*mark1calc+6*mark2calc+6*mark3calc+6*mark4calc+3*mark5calc)/30;
    marksem1.setText(Double.toString(totalsem1));
    marksem1.setText(String.format("%.2f", totalsem1));
} catch (Exception e) {
    // handle errors...
}

Upvotes: 0

Related Questions