4

I am having a problem in code it giving me error

Variable does not exist: val at line 14 column 25

I am sharing my code

public class UtilityClassCtrl    
{

public Integer val{get;set;}

    static String[] upto = new string[]{ 'zero', 'one',  'two', 'three', 'four',  'five',  'six',  
     'seven', 'eight', 'nine', 'ten',  'eleven', 'twelve', 'thirteen',  
      'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' };  
    static String[] tens = new string[]{ 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'};  
    static String[] denom = new string[]{ '','thousand',   'million', 'billion','trillion', 'quadrillion'};

    public static String convert_double_digit()
    {
        if(val<20)
            return upto[val];
        if(val==100)
            return 'One Hundred';
        for(Integer i=0;i<tens.size();i++)
        {
            String dcap=tens[i];

            integer dval=20+10*i;
            if(dval+10>val)
            {
                if(Math.mod(val,10)!=0)
                    return dcap+' '+upto[Math.mod(val,10)];
               return dcap;
            }

        }


            return 'Should never get here';

    } 

    public Static String convert_triple_digit()
    {
        String Word='';
        integer rem=val/100;
        integer mod=Math.mod(val,100);
        if(rem>0)
        {
            word=upto+'hundred';


        if(mod>0)
        {
           word+=''; 
        }

      }

         if(mod>0)
         {
             word+=convert_double_digit(mod);
         }

       return word;


    }

    public Static String english_number()
    {
        if(val<100)
        {
            return convert_double_digit(val.intValue());
        }

        if(val<1000)
        {
            return convert_triple_digit(val.intValue());
        }

        for(integer v=0;v<denom.size();v++)
        {
            integer didx=v-1;
            integer dval=(Integer)Math.pow(1000,v);
            if(dval>val)
            {
                integer mod=(integer)Math.pow(1000,didx);
                integer l=(integer) val/mode;
                integer r=(integer) val-(1*mod);
                String ret=convert_triple_digit(l)+' '+denom[didx];

                if(r>0)
                {
                    ret+=','+english_number(r);
                }

                return ret;
            }

        }
        return 'never get here';


    }


 }

1 Answer 1

4

You have your methods marked as static which means they can only reference fields in your class that are also marked static. So the quick fix is:

public static Integer val{get;set;}

But it is best to pass values into static methods via arguments like this (and remove the field):

public static String convert_double_digit(Integer val) {
    ...
}

not via static fields.

Using static fields to hold unchanging reference data such as your various arrays is appropriate. Adding the final keyword:

private static final String[] upto = new string[]{...};

helps people looking at your code understand that the data is unchanging reference data.

5
  • You beat me to it for a minute :) Jul 3, 2015 at 11:09
  • actually i have resolved this problem but i am getting error now inMethod does not exist or incorrect signature: [Integer].intValue() at line 67 column 41 Jul 3, 2015 at 11:13
  • @ShantanuMahajan If val is already an Integer you don;t need to invoke a method to get the Integer value.
    – Keith C
    Jul 3, 2015 at 11:20
  • @KeithC how i will call utiliy class method into another class. Jul 3, 2015 at 12:49
  • @ShantanuMahajan If all this is new to you I suggest you invest sometime in learning the basics through workbooks or tutorials. This forum isn't meant to be a place to get a series of questions answered. You can call one of the static methods in your class from another class by e.g. String s = UtilityClassCtrl.convert_double_digit(987);.
    – Keith C
    Jul 3, 2015 at 13:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .