Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In my javacode I am using while loop where a value is adding together and making it to a single digit .. for example if it is 2010 then 2+0+1+0 = 3 and if it is 2345 then 2+3+4+5 = 14 then 1 + 4 = 5 like that ... but at the same time this process is not valid for two numbers .. 11 & 22 if the coming value is 11 and 22 then no further addition like 1+1 =2 or 2+2 =4; they have to display it as both 11 and 22 ... I wrote code like below but it is not working.... anyone pls check the code and help me...what is the mistake...

private long getSum10(String text)
{
    long sum10 = 0;
    char[] name10 = new char[text.length()];
    name10 = text.toCharArray();
    for (int i = 0; i < text.length(); i++)
    {
        sum10 += value10(name10[i]);
    }
    while ((sum10 != 11) && (sum10 != 22) && (sum10 > 9))
    {
        sum10 = findDigitSum10(sum10);
    }
    return sum10;
}
private long findDigitSum10(long n)
{
    int sum10 = 0;
    while (n != 0)
    {
        sum10 += n % 10;
        n = n / 10;
    }
    return sum10;
}
private int value10(char a)
{
    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;
    }
}
share|improve this question
3  
what is value10? –  Amulya Khare Nov 6 '13 at 9:19
    
I will update it now.... –  roshanpeter Nov 6 '13 at 9:19
    
input is text as String and out is the sum of digits until the sum is in one digit , right ?? is that what you want ?? –  Hussain Akhtar Wahid 'Ghouri' Nov 6 '13 at 9:20
    
Post method value10(). –  Mr Smith Nov 6 '13 at 9:21
    
no.. My problem is with the while loop... when i gave like sum10>9 it was working perfectly... when I gave two condition like sum10!=11 and sum10!=22 it is not working correctly.. that is the problem ... so I need to do this process only when value is greater than 9 and not 11 and 22... –  roshanpeter Nov 6 '13 at 9:23

3 Answers 3

I would have implemented this in the following way

public static void main(String[] args) {
    System.out.println(getSum(12));
    System.out.println(getSum(11));
}
public static long getSum(int no){      
    if(no/10 == 0 || no%11 == 0){
        return no;
    }else{
        return getSum(no/10) + no%10;
    }
}
share|improve this answer

Put a condition to check the value of number, say if no. is greater than 99 the digits must add else show the number as it is.

Update your method like this

private long findDigitSum10(long n)

    {
        // TODO Auto-generated method stub

        int sum10 = 0;
        long temp = n;
        if (temp > 99) {

            while (n != 0) {
                sum10 += n % 10;
                n = n / 10;
            }
        }
        else{
            return n;
        }

        return sum10;

    }
share|improve this answer
    
value10(name10[i]) never returns value greater then 9 so while loop/findDigitSum10 is not executed at all –  Shakeeb Ayaz Nov 6 '13 at 9:31
 while ((sum10!=11) && (sum10!=22) && (sum10>9))
{
}

make it

if ((sum10 != 11) && (sum10 != 22) )
{
   sum10 = findDigitSum10(sum10);
}

change it to if condition.. I don see the reason why u have written it in while condition..the answer ur are goin to get from findDigitSum10(sum10) will alway be less than 10..

share|improve this answer
    
Because findDigitSum10(57) is 12, not 3. –  immibis Feb 23 at 3:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.