I am trying to write a program that takes an int as input. Then it prints out the number with spaces and the sum. Example:
input: 123
output: 1 2 3 The sum is: 6
here is the output I am getting: 1 The sum is: 6
here is my code
import java.util.*; public class ch5PrEx1 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { String input; int sum = 0, numericInput, length; System.out.println("Please enter an integer: "); System.out.println(); input = console.next(); numericInput = Integer.parseInt(input); numericInput = Math.abs(numericInput); input = Integer.toString(numericInput); length = input.length(); System.out.println(input.charAt(length-length) + " " + Addition(length, input) + " " + "The sum is: " + Sum(sum, length, numericInput)); } public static int Sum(int sum, int length, int numericInput) { int counter = 0; while(counter < length) { sum += numericInput % 10; numericInput /= 10; counter = counter + 1; } return sum; } public static String Addition(int length, String input) { String output = " ", index; int add = 0, counter = 0; while(counter < length) { index = input.substring(add + 1, add + 1); output = String.format("%s", index); add = add + 1; counter++; } return output; } }
Thanks in advance!