Performing calculations in ODK Build

Hi guys. Am having problems performing calculations using ODK Build. Like trying to sum up two numbers in numeric fields: txt_one and txt_two. Then displaying the results in numeric field, txt_three.
I have this in calculate box of txt_three.
${txt_one}+${txt_two}.
And I keep on getting an error when uploading the form to aggregate.
Am new to ODK. So any help is appreciated.

In ODK Build it doesn't work in the same way as in xls.
You need rater add something like:
/data/txt_one + /data/txt_two

the path depends on your groups.

2 Likes

Thanks for the response. Is data the name of the XML file?

No data is the name of the main section your question is inside. If you have another group, for example, you need to add it too.

In this case it would be /data/group1/text

<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
  <h:head>
    <h:title>testForm</h:title>
    <model>
      <instance>
        <data id="build_testForm_1524041259">
          <meta>
            <instanceID/>
          </meta>
          <group1>
            <text/>
          </group1>
        </data>
      </instance>
      <itext>
        <translation lang="English">
          <text id="/data/group1:label">
            <value>group1</value>
          </text>
          <text id="/data/group1/text:label">
            <value>Text</value>
          </text>
        </translation>
      </itext>
      <bind nodeset="/data/meta/instanceID" type="string" readonly="true()" calculate="concat('uuid:', uuid())"/>
      <bind nodeset="/data/group1/text" type="string"/>
    </model>
  </h:head>
  <h:body>
    <group>
      <label ref="jr:itext('/data/group1:label')"/>
      <input ref="/data/group1/text">
        <label ref="jr:itext('/data/group1/text:label')"/>
      </input>
    </group>
  </h:body>
</h:html>

Okay got you explanation on that. Let me try it out and will get back to you. Thanks

Alright here is my xml file. I keep on getting an error: "java.lang.boolean cannot be cast to java.lang.integer" when i run it on ODK collect.

<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
  <h:head>
    <h:title>Test Form one</h:title>
    <model>
      <instance>
        <data id="build_Untitled-Form_1524045635">
          <meta>
            <instanceID/>
          </meta>
          <num_one/>
          <num_two/>
          <calculate/>
        </data>
      </instance>
      <itext>
        <translation lang="English">
          <text id="/data/num_one:label">
            <value>enter first number</value>
          </text>
          <text id="/data/num_two:label">
            <value>enter second number</value>
          </text>
          <text id="/data/calculate:label">
            <value>Answer</value>
          </text>
        </translation>
      </itext>
      <bind nodeset="/data/meta/instanceID" type="string" readonly="true()" calculate="concat('uuid:', uuid())"/>
      <bind nodeset="/data/num_one" type="int"/>
      <bind nodeset="/data/num_two" type="int"/>
      <bind nodeset="/data/calculate" type="int" calculate="calculate=&quot;/data/num_one + /data/num_two&quot;"/>
    </model>
  </h:head>
  <h:body>
    <input ref="/data/num_one">
      <label ref="jr:itext('/data/num_one:label')"/>
    </input>
    <input ref="/data/num_two">
      <label ref="jr:itext('/data/num_two:label')"/>
    </input>
    <input ref="/data/calculate">
      <label ref="jr:itext('/data/calculate:label')"/>
    </input>
  </h:body>
</h:html>

Could you attach a screen from the ODK Build (which includes your calculation)?

Am using the offline version of ODK build. Below is the screen shot.

Please try just /data/num_one + /data/num_two in the calculate field.

Okay i finally got it to work! I replaced the expression in the calculate field with what you said. And in its Default Value field, i added this '/data/calculate' (without the quotes). Eureka!! worked like a charm. I have uploaded the xml file in case any one else runs in to the same problem.
Thanks @Grzesiek2010
Test Form One.xml (1.9 KB)

1 Like

Hi. Your advice for performing calculations in ODK build was really helpful. I wonder if you can help me verify if this constraint syntax in ODK build is correct. In this case Tested1st_HIVpos should be less than or equal to Tested_4HIV. Thank you,

constraint=". <= /data/HTS/Tested_4HIV" looks OK, but I note that your hint says "Your answer must be between 0 and 1000 or less than Tested for HIV..." whereas you actual constraint specifies and... So you might want to check that.

1 Like

The constraint should read as follows: Constraint = (/data/HTS/Tested1st_HIVpos) <= (/data/HTS/Tested_4HIV) in order to work as both fields are in a group named HTS.

October 17, 2018