Analyzing Integers from Text Files - Free Response Practice Question (with solution)


<-- Back to Interpreting River Data Next to Sum of Digits of Integers -->




Problem Statement

Write a Java program that reads integers (both positive and negative) from a file (named numbers2.txt) and does the following:

  1. Prints the line number of each number along with the number itself.

  2. Prints the sum of all the numbers read.

  3. Finds and prints the largest and smallest number.

    [Your program should NOT store the numbers in Array/ArrayList].

  4. Separate the numbers into two ArrayLists, one containing odd numbers and other containing even numbers, and print them.

Sample Input: numbers2.txt


Sample Output (a): Prints the line number of each number along with the number itself.

View Output

Solution for (a):

View Solution

ReadIntegersRunner.java

The program uses nextInt() to read one integer per iteration while a manually incremented line counter tracks its position. Both the line number and the integer value are printed together on each pass.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ReadIntegersRunner {

	public static void main(String args[]) {
		try {

			File inputFile = new File("numbers2.txt");
			Scanner input = new Scanner(inputFile);

			int line = 1;
			while (input.hasNext()) {
				int num = input.nextInt();
				System.out.println("Line "+line+":"+ num);
				line++;
			}
			input.close();

		} catch (FileNotFoundException e) {
			System.out.println("Error: File not found.");

		} catch (InputMismatchException e) {
			System.out.println("Error: Incomptabile data in file.");
		}

	}
}

Sample Output (b): Prints the sum of all the numbers read.

View Output

Solution for (b):

View Solution

SumIntegersRunner.java

Each integer is read with nextInt() and immediately added to a sum variable that accumulates across all iterations. The final sum, including both positive and negative values, is printed after the loop.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class SumIntegersRunner {

	public static void main(String args[]) {
		try {

			File inputFile = new File("numbers2.txt");
			Scanner input = new Scanner(inputFile);

			int sum = 0;
			while (input.hasNext()) {
				int num = input.nextInt();
				sum = sum + num;
			}
			System.out.println("The sum is: " + sum);
			input.close();

		} catch (FileNotFoundException e) {
			System.out.println("Error: File not found.");

		} catch (InputMismatchException e) {
			System.out.println("Error: Incomptabile data in file.");
		}

	}
}


Sample Output (c): Finds and prints the largest and smallest number.

[Your program should NOT store the numbers in Array/ArrayList].

View Output

Solution for (c):

View Solution

LargeAndSmallIntegerRunner.java

max is initialized to Integer.MIN_VALUE and min to Integer.MAX_VALUE so that the first number read always updates both — this handles negative numbers correctly without assuming the range. Each subsequent integer is compared against both trackers to keep them updated.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class LargeAndSmallIntegerRunner {

	public static void main(String args[]) {
		try {

			File inputFile = new File("numbers2.txt");
			Scanner input = new Scanner(inputFile);

			// cannot also be initialized to 0 in this case
			// since file has negative numbers
			int max = Integer.MIN_VALUE;
			int min = Integer.MAX_VALUE;

			while (input.hasNext()) {
				int num = input.nextInt();
				if (num > max)
					max = num;
				if (num < min)
					min = num;
			}
			System.out.println("The maximum number is: " + max);
			System.out.println("The minimum number is: " + min);
			input.close();

		} catch (FileNotFoundException e) {
			System.out.println("Error: File not found.");

		} catch (InputMismatchException e) {
			System.out.println("Error: Incomptabile data in file.");
		}

	}
}

Sample Output (d): Separate the numbers into two ArrayLists, one containing odd numbers and other containing even numbers, and print them.

View Output

Solution for (d):

View Solution

SeparateOddEvenIntegersRunner.java

Each integer read with nextInt() is tested with num % 2 == 0 to route it into either evenList or oddList. Both ArrayLists are printed in full after the file has been fully read.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class SeparateOddEvenIntegersRunner {

	public static void main(String args[]) {
		try {

			File inputFile = new File("numbers2.txt");
			Scanner input = new Scanner(inputFile);

			ArrayList evenList = new ArrayList();
			ArrayList oddList = new ArrayList();

			while (input.hasNext()) {
				int num = input.nextInt();

				if (num %2 ==0)
					evenList.add(Integer.valueOf(num));
				else
					oddList.add(Integer.valueOf(num));
			}
			System.out.println("Even numbers: " + evenList);
			System.out.println("Odd numbers: " + oddList);
			input.close();

		} 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 Interpreting River Data Next to Sum of Digits of Integers -->