In: Computer Science
Ask the user for a filename. In binary format, read all the integers in that file. Show the number of integers you read in, along with the maximum and minimum integer found. Language is Java.
/////////////input/////////
n1.dat
////////// output//////////
Enter a filename\n Found 50 integers.\n Max: 9944\n Min: 74\n
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class BinaryNumberFileInformation {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a filename");
File file = new File(in.nextLine());
try {
Scanner scanner = new Scanner(file);
int numIntegers = 0, num;
int small = Integer.MAX_VALUE;
int large = Integer.MIN_VALUE;
while (scanner.hasNextInt()) {
num = scanner.nextInt();
if (num > large)
large = num;
if (num < small)
small = num;
++numIntegers;
}
System.out.println("Found " + numIntegers + " integers.");
System.out.println("Max: " + large);
System.out.println("Min: " + small);
scanner.close();
} catch (IOException e) {
System.out.println(file.getAbsolutePath() + " does not exists!");
}
}
}