user1055656
user1055656

Reputation: 1

Android: Checking for empty EditText Fields

I am working on an android project for personal interest. I am working with multiple EditText fields and all must be filled in to a non-zero decimal or integer. I have specified in my xml file that it only accepts numbers or decimals. I am not too familiar with Java, but I have been reading into the try/catch method and how it is used when I am trying to parse each of the EditText values as a double. Right now, it runs fine when all of the fields are filled, but I want to add some kind of exception handling so that if by mistake you forget to enter a field, the application will display an error message instead of crashing. This sounds like a lot but I think it is pretty minor. Here is my calculating method that I am using. Thank you for your review :)

public static void calculate() {

    NumberFormat formatter1 = new DecimalFormat("#0.00");
    NumberFormat formatter2 = new DecimalFormat("#0.00000");

    Editable evG1F = etG1F.getText();
    Editable evG1U = etG1U.getText();
    Editable evG2F = etG2F.getText();
    Editable evG2U = etG2U.getText();
    Editable evG3F = etG3F.getText();
    Editable evG3U = etG3U.getText();
    Editable evWin = etWin.getText();

    double g1f = 0.0;
    double g1u = 0.0;
    double g2f = 0.0;
    double g2u = 0.0;
    double g3f = 0.0;
    double g3u = 0.0;
    double p1odds, p2odds, p3odds, p4odds, p5odds, p6odds, p7odds, p8odds;
    double totalParlay, profit, finalOdds, risk;

    win = 0.0;

    g1f = Double.parseDouble(evG1F.toString());
    g1u = Double.parseDouble(evG1U.toString());
    g2f = Double.parseDouble(evG2F.toString());
    g2u = Double.parseDouble(evG2U.toString());
    g3f = Double.parseDouble(evG3F.toString());
    g3u = Double.parseDouble(evG3U.toString());
    win = Double.parseDouble(evWin.toString());

    p1odds = (1 / (g1f * g2f * g3f));
    p2odds = (1 / (g1f * g2f * g3u));
    p3odds = (1 / (g1f * g2u * g3f));
    p4odds = (1 / (g1f * g2u * g3u));
    p5odds = (1 / (g1u * g2f * g3f));
    p6odds = (1 / (g1u * g2f * g3u));
    p7odds = (1 / (g1u * g2u * g3f));
    p8odds = (1 / (g1u * g2u * g3u));

    totalParlay = (p1odds + p2odds + p3odds + p4odds + p5odds + p6odds
            + p7odds + p8odds);
    profit = 1 - totalParlay;
    finalOdds = ((profit / totalParlay) + 1);
    risk = (win / (finalOdds - 1));

    p1 = ((risk + win) * p1odds);
    p2 = ((risk + win) * p2odds);
    p3 = ((risk + win) * p3odds);
    p4 = ((risk + win) * p4odds);
    p5 = ((risk + win) * p5odds);
    p6 = ((risk + win) * p6odds);
    p7 = ((risk + win) * p7odds);
    p8 = ((risk + win) * p8odds);

    sRisk = risk;
    sFinalOdds = finalOdds;

    tvRisk.setText("$" + formatter1.format(Parlay.sRisk));
    tvOdds.setText("" + formatter2.format(Parlay.sFinalOdds));

}

Upvotes: 0

Views: 999

Answers (1)

soren.qvist
soren.qvist

Reputation: 7416

You could wrap your parseDouble() statements in a try/catch block:

    try {
        g1f = Double.parseDouble(evG1F.getText().toString());
        g1u = Double.parseDouble(evG1U.getText().toString());
        g2f = Double.parseDouble(evG2F.getText().toString());
        g2u = Double.parseDouble(evG2U.getText().toString());
        g3f = Double.parseDouble(evG3F.getText().toString());
        g3u = Double.parseDouble(evG3U.getText().toString());
        win = Double.parseDouble(evWin.getText().toString());
    } catch (NumberFormatException e) {
        // Error handling here
    }

The exception will get thrown if it's not a valid double, or if the string is empty. I've added the getText() before toString() on each call, because you really don't need to declare those Editable objects up further (since you are only using getText() on them anyway).

Upvotes: 1

Related Questions