android - if statements possibly not working -




i have simple calculator 2 tabs, tabs appear working fine first tab calculating should, calculating second tab keeps throwing catch error.

in 2nd tab, have 2 if statements, checks see edittext field contains data. depending on contains data calculation does.

below code when button pressed have put together, streamlined i'm amature go easy on me.

any advice working please?

calculate.setonclicklistener(             new view.onclicklistener() {                 @override                 public void onclick(view view) {                       if ( tabs.getcurrenttab() == 0) {                          try {                              if (d.gettext().tostring().trim().length() == 0) {                                   a1 = a.gettext().tostring();                                 double a2 = double.parsedouble(a1);                                  b1 = b.gettext().tostring();                                 double b2 = double.parsedouble(b1);                                  double sum = (a2 * b2) / 10000;                                  double total = math.round((sum) * 10) / 10.0;                                  answer.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                 answer.settext(string.valueof("total area allocation = " + total + " ha"));                              } else if (d.gettext().tostring().trim().length() >= 1) {                                  a1 = a.gettext().tostring();                                 double a2 = double.parsedouble(a1);                                  b1 = b.gettext().tostring();                                 double b2 = double.parsedouble(b1);                                   d1 = d.gettext().tostring();                                 double d2 = double.parsedouble(d1);                                   double sum = (a2 * b2) / 10000;                                 double sum2 = (a2 * b2) / d2;                                  double total = math.round((sum) * 10) / 10.0;                                 double total2 = math.round((sum2) * 10) / 10.0;                                  answer.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                 answer.settext(string.valueof("total area allocation = " + total + " ha"));                                 answer2.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                 answer2.settext(string.valueof("set break " + total2 + " m"));                             }                         } catch (exception e) {                             answer.setbackgroundcolor(color.parsecolor("#d6ffffff"));                             answer.settext("enter values in input fields");                         }                     }                      if (tabs.getcurrenttab() == 1) {                          try {                              if (ff.gettext().tostring().trim().length() == 0) {                                   aa1 = aa.gettext().tostring();                                 double aa2 = double.parsedouble(aa1);                                  bb1 = bb.gettext().tostring();                                 double bb2 = double.parsedouble(bb1);                                  cc1 = cc.gettext().tostring();                                 double cc2 = double.parsedouble(cc1);                                  dd1 = dd.gettext().tostring();                                 double dd2 = double.parsedouble(dd1);                                  ee1 = ee.gettext().tostring();                                 double ee2 = double.parsedouble(ee1);                                   if (ee.gettext().tostring().trim().length() == 0){                                      double sum = (aa2 * bb2)/(cc2 - dd2);                                      double total = math.round((sum) * 10) / 10.0;                                      answer.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                     answer.settext(string.valueof("total area allocation = " + total + " ha"));                                  }                                  else if (cc.gettext().tostring().trim().length() == 0){                                      double sum = (aa2 * bb2)/ee2;                                      double total = math.round((sum) * 10) / 10.0;                                      answer.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                     answer.settext(string.valueof("total area allocation = " + total + " ha"));                                  }                               } else if (ff.gettext().tostring().trim().length() >= 1) {                                  aa1 = aa.gettext().tostring();                                 double aa2 = double.parsedouble(aa1);                                  bb1 = bb.gettext().tostring();                                 double bb2 = double.parsedouble(bb1);                                  cc1 = cc.gettext().tostring();                                 double cc2 = double.parsedouble(cc1);                                  dd1 = dd.gettext().tostring();                                 double dd2 = double.parsedouble(dd1);                                  ee1 = ee.gettext().tostring();                                 double ee2 = double.parsedouble(ee1);                                  ff1 = ff.gettext().tostring();                                 double ff2 = double.parsedouble(ff1);                                  if (ee.gettext().tostring().trim().length() == 0){                                      double sum = (aa2*bb2)/(cc2-dd2);                                     double sum2 = (sum * 10000) / ff2;                                      double total = math.round((sum) * 10) / 10.0;                                     double total2 = math.round((sum2) * 10) / 10.0;                                       answer.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                     answer.settext(string.valueof("total area allocation = " + total + " ha"));                                     answer2.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                     answer2.settext(string.valueof("set break " + total2 + " m"));                                  }                                  else if (cc.gettext().tostring().trim().length() == 0){                                      double sum = (aa2 * bb2)/ee2;                                     double sum2 = (sum * 10000) / ff2;                                      double total = math.round((sum) * 10) / 10.0;                                     double total2 = math.round((sum2) * 10) / 10.0;                                      answer.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                     answer.settext(string.valueof("total area allocation = " + total + " ha"));                                     answer2.setbackgroundcolor(color.parsecolor("#d6ffffff"));                                     answer2.settext(string.valueof("set break " + total2 + " m"));                                  }                              }                         } catch (exception e) {                             answer.setbackgroundcolor(color.parsecolor("#d6ffffff"));                             answer.settext("enter values in input fields 2");                         }                       }                 }               }); 

the catch error

while it's not strictly on topic of why you're running exceptions, might make suggestions improve code , reduce problems down road.

  1. try use more helpful variable names, it's hard know aa , bb are. try explain contain. numofcows or have you.
  2. move logic smaller functions. understand @ glance code does, , other code calls.

for example, create functions handling each tab's calculation, , move logic there.

 calculate.setonclicklistener(             new view.onclicklistener() {                 @override                 public void onclick(view view) {                     if (tabs.getcurrenttab() == 0) {                         calculatefirsttab();                     } else if (tabs.getcurrenttab() == 1) {                         calculatesecondtab();                     }                 }             }); 
  1. and lastly, try reduce duplication. have lot of code same thing, can make lot cleaner creating small functions help.

for example, here have created function called setanswer lets me pass in textview, , text. let set background color 1 place, , update text. then, creating function called setanswers lets pass in totals, can set both answers that.

private void setanswers( double total1, double total2 ){     setanswer(answer, "total area allocation = " + total1 + " ha");     setanswer(answer2, "set break " + total2 + " m"); }  private void setanswer( textview view, string text  ) {     view.setbackgroundcolor(color.parsecolor("#d6ffffff"));     view.settext(text); } 

there lot of other ways improve, suggestions may out.





wiki

Comments

Popular posts from this blog

python - Read npy file directly from S3 StreamingBody -

python - Minimize function with Scipy minimize -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -