Aulendil
Aulendil

Reputation: 9

A Java program that gets an integer, separates the individual digits and gives the sum of the digits

Below is my code, I have been able to get the sum of the digits, but I don't know how to cleanly separate and print the individual digits of my integer. Can it be done within my 'do' loop?

import java.util.*;

public class Ch5PgEx1 {

  static Scanner console = new Scanner(System.in);

  public static void main(String[] args) {

    int num;
    int sum;
    int remainder;
    int count;

    System.out.print("Please enter an integer: ");
    num = console.nextInt();
    System.out.println();

    sum = 0;
    count = 0;

    do {
        remainder = num % 10;
        sum = sum + remainder;
        num = num / 10;
        count++;
    }
    while (num > 0);


    System.out.print("There are " + count + " digits in your number. \nThe sum of the individual digits is:" + sum);
    System.out.println();
  } 
}

Upvotes: 0

Views: 126

Answers (3)

MS90
MS90

Reputation: 1249

Well, I have made a quick example for you. Take a look at it.

public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Pleaseee, input your numbers.");
        String text = scanner.nextLine();

        int[] arrayOfNumbers = new int[text.length()];

        for (int i = 0; i < text.length(); i++) {
            arrayOfNumbers[i] = text.charAt(i) - '0';
        }
        /*
        // prints every number
        for( int number : arrayOfNumbers){
            System.out.printf(" %s ",number);
        }
        */
        // sums every number
        int sumOfNumbers = IntStream.of(arrayOfNumbers).sum();

        String listOfNumbers = IntStream.of(arrayOfNumbers).mapToObj(Integer::toString).collect(Collectors.joining(", "));

        System.out.printf("Ho-ho-ho! List of numbers is: %s \n", listOfNumbers);
        System.out.printf("Ho-ho-ho! The sum of your numbers is: %d \n", sumOfNumbers);

    }

Upvotes: 0

Jo&#227;o Esperancinha
Jo&#227;o Esperancinha

Reputation: 1159

Not entirely sure if this would be something you'd be looking for. It does simplifies your code and it builds the array based on the remainders:

Integer Array

int arraySize = String.valueOf(num).length();
List<Integer> collectList = IntStream.range(0, arraySize).map(i -> (int) (num / Math.pow(10, i) % 10)).boxed()
        .collect(Collectors.toList());
Integer[] collect = collectList.toArray(new Integer[0]);

System.out.print("There are " + collectList.size()
        + " digits in your number. \nThe sum of the individual digits is:" + collectList.stream()
        .reduce(Integer::sum).orElse(0));
System.out.println();

Character Array

And if you want with characters, maybe this would also help you:

int arraySize = String.valueOf(num).length();
List<Integer> collectList = IntStream.range(0, arraySize).map(i -> (int) (num / Math.pow(10, i) % 10)).boxed()
        .collect(Collectors.toList());
Character[] collect = collectList.stream().map(number -> String.valueOf(number).charAt(0))
        .toArray(Character[]::new);

System.out.print("There are " + collectList.size()
        + " digits in your number. \nThe sum of the individual digits is:" + collectList.stream()
        .reduce(Integer::sum).orElse(0));
System.out.println();

Upvotes: 0

azro
azro

Reputation: 54148

  1. To print them just add the print line

    do {
        remainder = num % 10;
        sum = sum + remainder;
        num = num / 10;
        count++;
        System.out.println(remainder);
    } while (num > 0);
    
    /*
    Please enter an integer: 123456789
    
    9
    8
    7
    6
    5
    ...
    
  2. To store them in an array

    int[] res = new int[Integer.toString(num).length()];
    do {
        remainder = num % 10;
        sum = sum + remainder;
        num = num / 10;
        res[count++] = remainder;  // LINE 1
    } while (num > 0);
    
    /*
    Please enter an integer: 123456789
    
    [9, 8, 7, 6, 5, 4, 3, 2, 1]
    ...
    

    To have the array in [1, 2, 3, 4, 5, 6, 7, 8, 9] way :

    • way change LINE 1 to res[res.length - ++count] = remainder;

Upvotes: 1

Related Questions