Alan Vieira Rezende
Alan Vieira Rezende

Reputation: 412

EditText empty / Double

I have 4 Edit Text in my APP with hours and minutes

  double hour;
                    try {
                        hour= Double.parseDouble(dayhour.getText().toString().replace(',', '.'));
                    } catch (NumberFormatException e) {
                        hour = 0; 
                    }

  double minute;

                    try {
                        minute= Double.parseDouble(dayminute.getText().toString().replace(',', '.'));
                    } catch (NumberFormatException e) {
                        minute= 0; 
                    }
  double hour1;
                    try {
                    hour1= Double.parseDouble(dayhour1.getText().toString().replace(',', '.'));
                    } catch (NumberFormatException e) {
                    hour1= 0; 
                    }

  double minute1;

                    try {
                    minute1= Double.parseDouble(dayminute1.getText().toString().replace(',', '.'));
                    } catch (NumberFormatException e) {
                    minute1= 0; 
                    }

If the value of Edit Text hour, is not filled, what should I do to get the value that is in hour1? Same case with minute Edit Text.

Upvotes: 1

Views: 243

Answers (2)

Dhinakaran Thennarasu
Dhinakaran Thennarasu

Reputation: 3356

if(!TextUtils.isEmpty(dayhour.getText().toString()))
{
   hour= Double.parseDouble(dayhour.getText().toString().replace(',', '.'));
}
else
{
   hour= Double.parseDouble(dayhour1.getText().toString().replace(',', '.'));
   // hour = hour1; // if you already have the value of hour1.
}

same way for minutes and seconds

Upvotes: 0

iBobb
iBobb

Reputation: 1160

What if you made that change

  try {
                    hour= Double.parseDouble(dayhour.getText().toString().replace(',', '.'));
                } catch (NumberFormatException e) {
                    hour = null; 
                }

and then after all 4 blocks of code are executed you test:

if (hour==null) {
dayhour.setText("" + hour1);
}

Upvotes: 0

Related Questions