Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Rookie here, need help with sum of digits program

1. ## Rookie here, need help with sum of digits program

I am completely new to java and am very lost on a current program I am working on. I cannot figure out what code to add in or use when it comes to extracting the digits using an operator.

Here is my instructions:

Write a program that reads an integer from 0 to 999 from the keyboard and then adds up all the digits in the integer. For example, if the integer is 735 then the sum of the digits is 7+3+5=15.

(This is the current code I have so far) Any help is appreciated.

```1  import java.util.Scanner;
2   /**
3  *
4  * @author willkellogg
5  */
6  public class SumDigits {
7     public static void main(String[] args) {
8     Scanner input = new Scanner(System.in) ;
9
10      //input
11      System.out.println("Enter a number from 0 to 999: ") ;
12      double digit1 = input.nextDouble() ;
13      double digit2 = input.nextDouble() ;
14      double digit3 = input.nextDouble() ;
15
16      //process
17      double average  = (digit1 + digit2 + digit3) / 3 ;
18
19
20      //output
21      System.out.println("The sum of the digit is " + digit1 + "" + digit2 + ""
22                          + digit3 + "is" + average) ;

}
}```

2. ## Re: Rookie here, need help with sum of digits program

Your code is not following the assigment: program that reads an integer
The first fix for the program is to change what it reads from three doubles to one int.

The trick for isolating digits in a number is by using the % and / operators:
132 % 10 = 2
132 / 10 = 13

.