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.");
		}

	}
}
