1

I apologise if this has already been asked before, but I was unable to find a conclusive answer after some extensive searching, so I thought I would ask here. I am a beginner to Java (to coding, in general) and was tasked with writing a program that takes a user-inputted 3 digit number, and adds those three digits.

Note: I cannot use loops for this task, and the three digits must all be inputted at once.

String myInput;
    myInput =        
    JOptionPane.showInputDialog(null,"Hello, and welcome to the ThreeDigit program. "
    + "\nPlease input a three digit number below. \nThreeDigit will add those three numbers and     display their sum.");
    int threedigitinput;
    threedigitinput = Integer.parseInt(myInput);
1
  • 2
    Why do you want to create an array? Why not just pull each digit out of the input string and add them? This sounds like an XY problem to me. Commented Oct 11, 2014 at 21:49

4 Answers 4

2

There are a number of ways, one of which would be...

    String ss[] = "123".split("");
    int i = 
            Integer.parseInt(ss[0]) + 
            Integer.parseInt(ss[1]) + 
            Integer.parseInt(ss[2]);
    System.out.println(i);

another would be...

    String s = "123";
    int i = 
            Character.getNumericValue(s.charAt(0)) +
            Character.getNumericValue(s.charAt(1)) +
            Character.getNumericValue(s.charAt(2));
    System.out.println(i);

and still another would be...

    String s = "123";
    int i = 
            s.charAt(0) +
            s.charAt(1) +
            s.charAt(2) - 
            (3 * 48);
    System.out.println(i);

BUT hard coding for 3 numbers isn't very useful beyond this simple case. So how about recursion??

public static int addDigis(String s) {
        if(s.length() == 1)
            return s.charAt(0) - 48;
        return s.charAt(0) - 48 + addDigis(s.substring(1, s.length()));
}

Output for each example: 6

Sign up to request clarification or add additional context in comments.

Comments

0

you can use integer math to come up with the three numbers seperately

int first = threedigitinput / 100;
int second = (threedigitinput % 100) / 10;
int third = threedigitinput % 10;

2 Comments

an overkill, given that he can simply extract the digits and add them.
i know but this is the first thing that came to my mind with what the question already had done
0

If I understand your question, you could use Character.digit(char,int) to get the value for each character with something like -

int value = Character.digit(myInput.charAt(0), 10)
        + Character.digit(myInput.charAt(1), 10)
        + Character.digit(myInput.charAt(2), 10);

Comments

0

Classic example of using divmod:

public class SumIntegerDigits {
    public static void main(String[] args) {
        System.out.println(sumOfDigitsSimple(248));    // 14
        System.out.println(sumOfDigitsIterative(248)); // 14
        System.out.println(sumOfDigitsRecursive(248)); // 14
    }

    // Simple, non-loop solution
    public static final int sumOfDigitsSimple(int x) {
        int y = x % 1000; // Make sure that the value has no more than 3 digits.
        return divmod(y,100)[0]+divmod(divmod(y,100)[1],10)[0]+divmod(y,10)[1];
    }

    // Iterative Solution
    public static final int sumOfDigitsIterative(int x) {
        int sum = 0;
        while (x > 0) {
            int[] y = divmod(x, 10);
            sum += y[1];
            x = y[0];
        }
        return sum;
    }

    // Tail-recursive Solution
    public static final int sumOfDigitsRecursive(int x) {
        if (x <= 0) {
            return 0;
        }
        int[] y = divmod(x, 10);
        return sumOfDigitsRecursive(y[0]) + y[1];
    }

    public static final int[] divmod(final int x, int m) {
        return new int[] { (x / m), (x % m) };
    }
}

1 Comment

no loops as he said. simple string parsing and adding should do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.