How to write a complex average equation in android? -
i have application calculates 2 types of average, 1 simple, , second more complex , can't figure out how write equation of it. way have written how should calculated, error on syntax "{". equation should this: sum of values divided number of how many entered, result multiplied 3 plus value, , then, result divided 4. hope understood exactly. ! !
here my:
equation:
double total2 = {[(i + j + k + l + m + n + o + p + q + r)/numarvalori2] x 3 + r}/(double)4;
happens when try calulate first average, not second !!! medii.java:
package com.cngcnasaud.orar; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.*; public class medii extends activity { edittext txtnum1; edittext txtnum2; edittext txtnum3; edittext txtnum4; edittext txtnum5; edittext txtnum6; edittext txtnum7; edittext txtnum8; edittext txttotal; edittext txtnum10; edittext txtnum11; edittext txtnum12; edittext txtnum13; edittext txtnum14; edittext txtnum15; edittext txtnum16; edittext txtnum17; edittext txtnum18; edittext txtnum19; edittext txtnum20; edittext txttotal2; button btncal; button btncal2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.medii); txtnum1 = (edittext) findviewbyid(r.id.edittext1); txtnum2 = (edittext) findviewbyid(r.id.edittext2); txtnum3 = (edittext) findviewbyid(r.id.edittext3); txtnum4 = (edittext) findviewbyid(r.id.edittext4); txtnum5 = (edittext) findviewbyid(r.id.edittext5); txtnum6 = (edittext) findviewbyid(r.id.edittext6); txtnum7 = (edittext) findviewbyid(r.id.edittext7); txtnum8 = (edittext) findviewbyid(r.id.edittext8); txttotal = (edittext) findviewbyid(r.id.edittext9); btncal = (button) findviewbyid(r.id.bmedii); txtnum10 = (edittext) findviewbyid(r.id.edittext10); txtnum11 = (edittext) findviewbyid(r.id.edittext11); txtnum12 = (edittext) findviewbyid(r.id.edittext12); txtnum13 = (edittext) findviewbyid(r.id.edittext13); txtnum14 = (edittext) findviewbyid(r.id.edittext14); txtnum15 = (edittext) findviewbyid(r.id.edittext15); txtnum16 = (edittext) findviewbyid(r.id.edittext16); txtnum17 = (edittext) findviewbyid(r.id.edittext17); txtnum18 = (edittext) findviewbyid(r.id.edittext18); txtnum19 = (edittext) findviewbyid(r.id.edittext19); txttotal2 = (edittext) findviewbyid(r.id.edittext20); btncal2 = (button) findviewbyid(r.id.bmedii12); btncal.setonclicklistener(new clickbutton ()); btncal2.setonclicklistener(new clickbutton()); } private class clickbutton implements button.onclicklistener{ @override public void onclick(view v) { int = txtnum1.gettext().tostring().isempty()?0:integer.parseint(txtnum1.gettext().tostring()); int b = txtnum2.gettext().tostring().isempty()?0:integer.parseint(txtnum2.gettext().tostring()); int c = txtnum3.gettext().tostring().isempty()?0:integer.parseint(txtnum3.gettext().tostring()); int d = txtnum4.gettext().tostring().isempty()?0:integer.parseint(txtnum4.gettext().tostring()); int e = txtnum5.gettext().tostring().isempty()?0:integer.parseint(txtnum5.gettext().tostring()); int f = txtnum6.gettext().tostring().isempty()?0:integer.parseint(txtnum6.gettext().tostring()); int g = txtnum7.gettext().tostring().isempty()?0:integer.parseint(txtnum7.gettext().tostring()); int h = txtnum8.gettext().tostring().isempty()?0:integer.parseint(txtnum8.gettext().tostring()); int numarvalori = (a == 0 ? 0 : 1) + (b == 0 ? 0 : 1) + (c == 0 ? 0 : 1) + (d == 0 ? 0 : 1) + (e == 0 ? 0 : 1) + (f == 0 ? 0 : 1) + (g == 0 ? 0 : 1) + (h == 0 ? 0 : 1); double total = (a + b + c + d + e + f + g + h)/(double)numarvalori; txttotal.settext(double.tostring(total)); int = txtnum10.gettext().tostring().isempty()?0:integer.parseint(txtnum10.gettext().tostring()); int j = txtnum11.gettext().tostring().isempty()?0:integer.parseint(txtnum11.gettext().tostring()); int k = txtnum12.gettext().tostring().isempty()?0:integer.parseint(txtnum12.gettext().tostring()); int l = txtnum13.gettext().tostring().isempty()?0:integer.parseint(txtnum13.gettext().tostring()); int m = txtnum14.gettext().tostring().isempty()?0:integer.parseint(txtnum14.gettext().tostring()); int n = txtnum15.gettext().tostring().isempty()?0:integer.parseint(txtnum15.gettext().tostring()); int o = txtnum16.gettext().tostring().isempty()?0:integer.parseint(txtnum16.gettext().tostring()); int p = txtnum17.gettext().tostring().isempty()?0:integer.parseint(txtnum17.gettext().tostring()); int q = txtnum18.gettext().tostring().isempty()?0:integer.parseint(txtnum18.gettext().tostring()); int r = txtnum19.gettext().tostring().isempty()?0:integer.parseint(txtnum19.gettext().tostring()); int numarvalori2 = (i == 0 ? 0 : 1) + (j == 0 ? 0 : 1) + (k == 0 ? 0 : 1) + (l == 0 ? 0 : 1) + (m == 0 ? 0 : 1) + (n == 0 ? 0 : 1) + (o == 0 ? 0 : 1) + (p == 0 ? 0 : 1) + (q == 0 ? 0 : 1) + (r == 0 ? 0 : 1); double total2 = {[(i + j + k + l + m + n + o + p + q + r)/numarvalori2] x 3 + r}/(double)4; } } }
l.e:
logcat:
03-23 12:52:26.488: e/androidruntime(938): fatal exception: main 03-23 12:52:26.488: e/androidruntime(938): java.lang.arithmeticexception: divide 0 03-23 12:52:26.488: e/androidruntime(938): @ com.cngcnasaud.orar.medii$clickbutton.onclick(medii.java:99) 03-23 12:52:26.488: e/androidruntime(938): @ android.view.view.performclick(view.java:4204) 03-23 12:52:26.488: e/androidruntime(938): @ android.view.view$performclick.run(view.java:17355) 03-23 12:52:26.488: e/androidruntime(938): @ android.os.handler.handlecallback(handler.java:725) 03-23 12:52:26.488: e/androidruntime(938): @ android.os.handler.dispatchmessage(handler.java:92) 03-23 12:52:26.488: e/androidruntime(938): @ android.os.looper.loop(looper.java:137) 03-23 12:52:26.488: e/androidruntime(938): @ android.app.activitythread.main(activitythread.java:5041) 03-23 12:52:26.488: e/androidruntime(938): @ java.lang.reflect.method.invokenative(native method) 03-23 12:52:26.488: e/androidruntime(938): @ java.lang.reflect.method.invoke(method.java:511) 03-23 12:52:26.488: e/androidruntime(938): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 03-23 12:52:26.488: e/androidruntime(938): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 03-23 12:52:26.488: e/androidruntime(938): @ dalvik.system.nativestart.main(native method)
line 99:
double total2 = (((i + j + k + l + m + n + o + p + q + r)/numarvalori2) * 3 + r)/(double)4;
** happens why calculate first average, not second !
just replace []
, {}
()
in equation, can use () because [] , {} have special meaning in java.
edit 1:
the logcat output tells pretty problem is:
java.lang.arithmeticexception: divide 0
you diving 0 , since variable diving numarvalori
, numarvalori2
1 of these has 0.
what think happening this: when click calculate button performing both calculations. when nothing has been entered second calculation every input variable defaults 0 , when variables 0 numarvalori2
. when calculation performed divide zero.
i can suggest few changes code improve it, give me few minutes , edit answer.
edit 2:
here improved source code. didn't test (i if find time in half hour) should work:
import android.app.activity; import android.os.bundle; import android.text.editable; import android.view.view; import android.widget.button; import android.widget.edittext; public class medii extends activity { private edittext txtnum1; private edittext txtnum2; private edittext txtnum3; private edittext txtnum4; private edittext txtnum5; private edittext txtnum6; private edittext txtnum7; private edittext txtnum8; private edittext txttotal; private edittext txtnum10; private edittext txtnum11; private edittext txtnum12; private edittext txtnum13; private edittext txtnum14; private edittext txtnum15; private edittext txtnum16; private edittext txtnum17; private edittext txtnum18; private edittext txtnum19; private edittext txtnum20; private edittext txttotal2; private button btncal; private button btncal2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.medii); txtnum1 = (edittext) findviewbyid(r.id.edittext1); txtnum2 = (edittext) findviewbyid(r.id.edittext2); txtnum3 = (edittext) findviewbyid(r.id.edittext3); txtnum4 = (edittext) findviewbyid(r.id.edittext4); txtnum5 = (edittext) findviewbyid(r.id.edittext5); txtnum6 = (edittext) findviewbyid(r.id.edittext6); txtnum7 = (edittext) findviewbyid(r.id.edittext7); txtnum8 = (edittext) findviewbyid(r.id.edittext8); txttotal = (edittext) findviewbyid(r.id.edittext9); btncal = (button) findviewbyid(r.id.bmedii); txtnum10 = (edittext) findviewbyid(r.id.edittext10); txtnum11 = (edittext) findviewbyid(r.id.edittext11); txtnum12 = (edittext) findviewbyid(r.id.edittext12); txtnum13 = (edittext) findviewbyid(r.id.edittext13); txtnum14 = (edittext) findviewbyid(r.id.edittext14); txtnum15 = (edittext) findviewbyid(r.id.edittext15); txtnum16 = (edittext) findviewbyid(r.id.edittext16); txtnum17 = (edittext) findviewbyid(r.id.edittext17); txtnum18 = (edittext) findviewbyid(r.id.edittext18); txtnum19 = (edittext) findviewbyid(r.id.edittext19); txttotal2 = (edittext) findviewbyid(r.id.edittext20); btncal2 = (button) findviewbyid(r.id.bmedii12); btncal.setonclicklistener(new simpleaverageclicklistener()); btncal2.setonclicklistener(new complexaverageclicklistener()); } private double getnumberfromedittext(edittext edittext, double defaultvalue) throws numberformatexception { if (edittext != null) { editable editable = edittext.gettext(); if (editable != null) { return double.parsedouble(editable.tostring()); } } return defaultvalue; } private boolean isedittextempty(edittext edittext) { if (edittext != null) { editable editable = edittext.gettext(); if (editable != null) { return editable.tostring().isempty(); } } return true; } private double getaverageofedittexts(edittext... edittexts) throws numberformatexception{ double divider = 0.0; double sum = 0.0; (edittext edittext : edittexts) { if (!isedittextempty(edittext)) { divider++; sum += getnumberfromedittext(edittext, 0.0); } } return sum / divider; } private class simpleaverageclicklistener implements button.onclicklistener { @override public void onclick(view v) { try { double average = getaverageofedittexts(txtnum1, txtnum2, txtnum3, txtnum4, txtnum5, txtnum6, txtnum7, txtnum8); txttotal.settext(string.valueof(average)); } catch (numberformatexception e) { // exception occurred because of illegal input, nothing. } } } private class complexaverageclicklistener implements button.onclicklistener { @override public void onclick(view v) { try { double average = getaverageofedittexts(txtnum10, txtnum11, txtnum12, txtnum13, txtnum14, txtnum15, txtnum16, txtnum17, txtnum18); double total = (average * 3 + getnumberfromedittext(txtnum19, 0.0)) / 4.0; txttotal2.settext(string.valueof(total)); } catch (numberformatexception e) { // exception occurred because of illegal input, nothing. } } } }
Comments
Post a Comment