Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java method

DJJONES91
DJJONES91 


Write a Java program that will input from the user several positive integers (you decide what type of loop to use: count-controlled or sentinel-controlled), and then will call a method to sum the digits in any positive integer. Hint: use the division and modulus operators. Consider the fact that the last digit of an integer n is n%10 and the number consisting of all but the last digit is given by n/10. Note #1: Your program should use modularization. Note #2: Write a second version of this program and read numbers from an input file. Show the number and the sum of all digits on the screen. Get the file name from the user, handle the FileNotFoundException exception (try/catch) and consume unwanted input Attachments
Tags Clarifications

Answers

Java - Sum Digits From User And File - Solution

dickersonka
dickersonka 



Attached is the solution to the java sum digits from user and file including source and comments Attachments
SumNumbers.zip (1 K) Download
Preview
of all digits in number * @param number number to get sum of digit * @return Sum of digits */ private static int getDigitSum(Integer number){ int digSum = 0; //assign parameter to temp int temp = number; //continue looping while not

Preview
int sum = 0; String sNumber; try { //loop until end of file while((sNumber = br.readLine()) != null){ //display line read in System.out.println("Read: " + sNumber); try{ //Parse number from file int number = Integer.parseInt(sNumber);

Preview
123 456

Screenshots
Purchase Answer