Free Response Practice Question (with solution)- Analyzing Integers from Text Files
| <-- Back to Interpreting River Data | Next to AP CSA Exam Solutions --> |
Analyzing Integers from Text Files
Write a Java program that reads integers (both positive and negative) from a file (named numbers2.txt) and does the following:
- Prints the line number of each number along with the number itself.
- Prints the sum of all the numbers read.
- Finds and prints the largest and smallest number.
[Your program should NOT store the numbers in Array/ArrayList].
- 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 SolutionReadIntegersRunner.java
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 SolutionSumIntegersRunner.java
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 SolutionLargeAndSmallIntegerRunner.java
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 SolutionSeparateOddEvenIntegersRunner.java
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):
- numbers2.txt
- ReadIntegersRunner.java
- SumIntegersRunner.java
- LargeAndSmallIntegerRunner.java
- SeparateOddEvenIntegersRunner.java
| <-- Back to Interpreting River Data | Next to AP CSA Exam Solutions --> |