Finding sum of digits of integers read from Text Files with and without using ArrayLists (or Arrays) - Free Response Practice Question (with solution)
| <-- Back to Analyzing Integers | Next to Reversing Digits of Integers --> |
Problem Statement
Write a Java program that reads positive integers from a file (named sumOfDigits.txt) and does the following:
- Store all the numbers in an ArrayList. Then, calculate the sum of digits of each number and store these sums in another ArrayList. Find the number with maximum sum of digits.
- Implement (a) without storing the numbers or sums in any Array or ArrayList.
Sample Input: sumOfDigits.txt
Sample Output (a): Store all the numbers in an ArrayList. Then, calculate the sum of digits of each number and store these sums in another ArrayList. Find the number with maximum sum of digits.
View Output
Solution for (a):
View SolutionMaxSumOfDigits.java
All integers are first collected into numbersList, then a second loop calls the helper getSumOfDigits() for each and stores the results in sumList. A final scan through sumList finds the index of the largest digit sum and uses it to look up the corresponding original number.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MaxSumOfDigits {
public static int getSumOfDigits(int n) {
n = Math.abs(n); // handle negative numbers
int sum = 0;
while (n > 0) {
sum = sum + (n % 10);
n = n / 10;
}
return sum;
}
public static void main(String args[]) {
try {
File inputFile = new File("sumOfDigits.txt");
Scanner input = new Scanner(inputFile);
ArrayList numbersList = new ArrayList();
ArrayList sumList = new ArrayList();
int num = 0;
int sum = 0;
while (input.hasNext()) {
num = input.nextInt();
numbersList.add(Integer.valueOf(num));
}
input.close();
// loop through numbersList
for (int i = 0; i < numbersList.size(); i++) {
sum = getSumOfDigits(numbersList.get(i));
sumList.add(Integer.valueOf(sum));
}
// find max digit sum & corresponding number
int maxSum = sumList.get(0);
int maxIndex = 0;
for (int i = 1; i < sumList.size(); i++) {
if (sumList.get(i) > maxSum) {
maxSum = sumList.get(i);
maxIndex = i;
}
}
System.out.println("Number with maximum digit sum = " + numbersList.get(maxIndex));
System.out.println("Its digit sum = " + maxSum);
} catch (FileNotFoundException e) {
System.out.println("Error: File not found.");
} catch (InputMismatchException e) {
System.out.println("Error: Incomptabile data in file.");
}
}
}
Sample Output (b): Implement (a) without storing the numbers or sums in any Array or ArrayList.
View Output
Solution for (b):
View SolutionMaxSumOfDigitsWithoutStoring.java
Rather than storing numbers in an ArrayList, getSumOfDigits() is called immediately for each integer as it is read. A running maxSum and its corresponding maxNum are updated on the fly whenever a larger digit sum is found.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MaxSumOfDigitsWithoutStoring {
public static int getSumOfDigits(int n) {
n = Math.abs(n); // handles negative numbers
int sum = 0;
while (n > 0) {
sum = sum + (n % 10);
n = n / 10;
}
return sum;
}
public static void main(String args[]) {
try {
File inputFile = new File("sumOfDigits.txt");
Scanner input = new Scanner(inputFile);
int num = 0;
int sum = 0;
int maxSum = Integer.MIN_VALUE;
int maxNum = 0;
while (input.hasNext()) {
num = input.nextInt();
sum = getSumOfDigits(num);
if (sum > maxSum) {
maxSum = sum;
maxNum = num;
}
}
input.close();
System.out.println("Number with maximum digit sum = " + maxNum);
System.out.println("Its digit sum = " + maxSum);
} catch (FileNotFoundException e) {
System.out.println("Error: File not found.");
} catch (InputMismatchException e) {
System.out.println("Error: Incomptabile data in file.");
}
}
}
Java project files (with input files):
| <-- Back to Analyzing Integers | Next to Reversing Digits of Integers --> |