8 Replies Latest reply on Nov 14, 2015 5:18 PM by jboyk

    Syntax error 8 at line 9

    jboyk

      Hi,

       

      I'm trying to run a script used to calculate the value of a field based on conditions. It should look to see if the checkbox, ForceKno, is checked and then check to see if "1f2" does not have a value of 5. If both of those conditions are true, it should return the value of field "1T". If any other combination of conditions are true, it should return the value of the sum of the fields 1f2 + 1f3 + 1f4 + MOD KNO + 1/2 Level

       

      I know almost nothing about Java or coding and pieced it together using forum help and web sites that addressed similar types of errors. I think I copied the syntax of the code, however I keep getting errors. Here is the code:

       

      var c=getField("ForceKno").value;

      var o=getField("1f2").value;

      var v1 = getField("1f2").value;

      var v2 = getField("1f3").value;

      var v3 = getField("1f4").value;

      var v4 = getField("MOD KNO").value;

      var v5 = getField("1/2 Level").value;

      if ( ( c=="1" ) && ( o<>"5" ) ) event.value = getField("1T").value;

      else {

      event.value = (v1 + v2 + v3 + v4 + v5);

      }

        • 1. Re: Syntax error 8 at line 9
          try67 MVP & Adobe Community Professional

          The syntax error is that you're using the wrong operator for "not equals". It needs to be "!=", not "<>".

          Also, you should remove the quotes around the numbers in that if-condition.

          • 2. Re: Syntax error 8 at line 9
            jboyk Level 1

            I'm allowed to accept that code, however it isn't functioning correctly. I made the following changes (and tried without "" around the 1 and 5 in the if statement. What happens is that is will change the number to 1T if it matches those conditions, but won't change back to sum if the conditions stop being met.

             

            var c=getField("ForceKno").value;

            var o=getField("1f2").value;

             

             

            if ( ( c=="1" ) && ( o!="5" ) ) {

             

             

            event.value=getField("1T").value;

             

             

            }

             

             

            else {

             

             

            var v1 = +getField("1f2").value;

            var v2 = +getField("1f3").value;

            var v3 = +getField("1f4").value;

            var v4 = +getField("MOD KNO").value;

            var v5 = +getField("1/2 Level").value;

             

             

            // Perform the addition

            var sum = v1 + v2 + v3 + v4 + v5;

             

             

            event.value = sum;

             

             

            }

            • 3. Re: Syntax error 8 at line 9
              gkaiseril MVP & Adobe Community Professional

              Are there any fields with an empty value or null value? You should be aware that the "+" operator is either the addition operator for numerical values for summing numeric values or the concatenation operator for string values or numeric values being combined with string values. A "null" value is treated like a string value unless forced to a numeric value by adjusting the null value by multiplying by one, dividing by one, using the "+" as a prefix for the variable, or using the Number constrictor,

               

              If you set the format for the result field to "Number" or 'Percent" then the value of 1T will not be accepted because of a format error.

              • 4. Re: Syntax error 8 at line 9
                jboyk Level 1

                There are null values. Are you saying I could write var sum = (v1/1) +( v2/2)........

                 

                Is there a way to change the "var 5 = +getfield("1/2 Level").value; to turn null values into numbers?

                 

                 

                Last question; the if statement o!="5"       The value of o would either be 5 or null (blank), so is there something wrong in that code?

                 

                I know nothing about Java, I strung this together from using context clues on other posts, websites, and numerous google searches. I'm glad you guys are explaining why, that helps me learn. Problem is I need an example of what exactly it should look like.

                • 5. Re: Syntax error 8 at line 9
                  gkaiseril MVP & Adobe Community Professional

                  First the language used by Acrobat is JavaScript, an interpreted language like basic and not JAVA, a complied language.

                   

                  You would be much better of multiplying by 1 or using the "+" prefix.

                   

                  event.value = +v1 + +v2 + +v3 + +v4 + +v5;

                   

                  or the Number constrictor:

                   

                  event.value = Number(v1) + Number(v2) + Number(v3) + Number(v4) + Number(v5);

                   

                  Either method would force the null value to 0.

                   

                  It is not a good idea to quotations around numeric values in script. The null value is a script is "".

                   

                  Since all the values for the fields are placed in a variable, why not just force the values of the fields at that time to numeric values?

                  • 6. Re: Syntax error 8 at line 9
                    jboyk Level 1

                    Ok, now I have this; and it still isn't working.

                     

                    var c=getField("ForceKno").value;

                    var o= 1*getField("1f2").value;

                    var v1 = 1*getField("1f2").value;

                    var v2 = 1*getField("1f3").value;

                    var v3 = 1*getField("1f4").value;

                    var v4 = 1*getField("MOD KNO").value;

                    var v5 = 1*getField("1/2 Level").value;

                    if ( ( c=="1" ) && ( o!="5" ) ) event.value = getField("1T").value;

                    else {

                    event.value = (+v1 + +v2 + +v3 + +v4 + +v5);

                    }

                     

                     

                    Please, if someone will just fix this code, I can figure out the why by seeing the change. I feel like an idiot because I keep changing things and don't seem to get anywhere. When I remove all the quotations from around everything, I get different errors.

                    • 7. Re: Syntax error 8 at line 9
                      try67 MVP & Adobe Community Professional

                      Let me put you out of your misery... Assuming your logic is correct, this is the code you should use:

                      var c = Number(this.getField("ForceKno").value);
                      var o = Number(this.getField("1f2").value);
                      var v1 = Number(this.getField("1f2").value);
                      var v2 = Number(this.getField("1f3").value);
                      var v3 = Number(this.getField("1f4").value);
                      var v4 = Number(this.getField("MOD KNO").value);
                      var v5 = Number(this.getField("1/2 Level").value);
                      
                      if (c==1 && o!=5) event.value = this.getField("1T").value;
                      else event.value = (v1 + v2 + v3 + v4 + v5);
                      
                      • 8. Re: Syntax error 8 at line 9
                        jboyk Level 1

                        Thank you, I really appreciate the help both of you gave me.

                         

                        Try67 - thanks for showing me the code, I can see now how it should look and it worked perfectly!