Question

In my android code.. I need to add the digits of a number together and check whether the value is equal to numbers 13,14,16 and 19.. So the app is about numerology where when user enter his first name.. each letter has a value between 1-9 so all these values are taken and the values are added together... and will check if the value is 13,14,16 or 19.. I am giving my code below... but the code is giving a single digit ..only ..

mainactivity

long kar1 = 0;
kar1 = getKar1(et1.getText().toString();

private long getKar1(String text) {
        // TODO Auto-generated method stub
         long kar1 = 0;
            char[] karmi1 = new char[text.length()];
                   karmi1 = text.toCharArray();               
                   for(int i=0; i<text.length(); i++)
                   {
                       kar1 += value1( karmi1[i] );

                   }

                   while ((kar1!= 13)||(kar1!=14)||(kar1!=16)||(kar1!=19))
                   {
                       kar1 = findSum(kar1);
                   }

                return kar1;

    }







private int value1(char a) 

    {
        // TODO Auto-generated method stub
        switch(a)
        {
           case 'A':return 1;    
           case 'B':return 2;
           case 'C':return 3;
           case 'D':return 4;
           case 'E':return 5;
           case 'F':return 6;
           case 'G':return 7;
           case 'H':return 8;
           case 'I':return 9;
           case 'J':return 1;
           case 'K':return 2;
           case 'L':return 3;
           case 'M':return 4;
           case 'N':return 5;
           case 'O':return 6;
           case 'P':return 7;
           case 'Q':return 8;
           case 'R':return 9;
           case 'S':return 1;          
           case 'T':return 2;
           case 'U':return 3;
           case 'V':return 4;
           case 'W':return 5;
           case 'X':return 6;
           case 'Y':return 7;
           case 'Z':return 8;
           default: return 0;   
        }   
    }




private long findSum(long n) 

    {
        // TODO Auto-generated method stub

        int kar1 = 0;
        while (n != 0)
        {
         kar1 += n % 10;
         n = n / 10;
        }          

        return kar1;
    }
Was it helpful?

Solution

Try this code

use this piece of code to get sum of digits of number and then check in if condition

   String S=et1.getText().toString();
   int i,a=0, sum=0;
   i=Integer.parseInt(S);          
   while(i!=0)
   {
       a=i%10;
       i=i/10;
       sum=sum+a;
   }
   System.out.println(sum);
   if(sum==13||sum==14||sum==16||sum==19)
   {
       System.out.println("match");
   }
   else
   {
       System.out.println("not match");
   }

OTHER TIPS

Use this method to find the total of the String and use it's return value for comparision:

int total(String str){
    String s = str.toUpperCase();
    int total = 0;
    char[] c = new char[s.length()];
    s.getChars(0, s.length(), c, 0);
    for(int i = 0; i<str.length();i++)
        total += c[i] - 'A' + 1;
    return total;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top