Question

In: Computer Science

Ask the user for a filename. In binary format, read all the integers in that file....

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

Solutions

Expert Solution

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

Related Solutions

Ask the user for a filename, then ask the user for a line of text. Write...
Ask the user for a filename, then ask the user for a line of text. Write the line of text the filename specified by the user. The filename may be absolute or relative to the project directory.
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
1. Write a program that prompts the user for a filename, then reads that file in...
1. Write a program that prompts the user for a filename, then reads that file in and displays the contents backwards, line by line, and character-by character on each line. You can do this with scalars, but an array is much easier to work with. If the original file is: abcdef ghijkl the output will be: lkjihg fedcba Need Help with this be done in only PERL. Please use "reverse"
In java, ask the user for a filename. Display the oldest car for every manufacturer from...
In java, ask the user for a filename. Display the oldest car for every manufacturer from that file. Files Given: car-list.txt car-list-1.txt car-list-2.txt car-list-3.txt (they are too big for question) Required Output: Standard Input                 Files in the same directory car-list-1.txt car-list.txt car-list-1.txt car-list-2.txt car-list-3.txt Enter filename\n Oldest cars by make\n Acura Legend 1989 2T1BPRHE8EC858192\n Audi 80/90 1988 2GKALMEK4E6424961\n Bentley Continental Flying Spur 2006 WBA3G7C5XFK430850\n BMW 600 1959 1N6AA0ED2FN639969\n Bugatti Veyron 2009 SCFEBBBC5AG474636\n Buick Century 1987 2HNYD18625H455550\n Cadillac DeVille 1994...
Read in user-input integers one at a time until the user types ‘q’, storing all inputs...
Read in user-input integers one at a time until the user types ‘q’, storing all inputs in a list. Then, print out the even numbers in increasing order, from smallest value to largest. Python
Read three integers from user input.
Read three integers from user input.
Script 3: Ask the user for a file's name If the file exists, ask them if...
Script 3: Ask the user for a file's name If the file exists, ask them if they would like to (C)opy, (M)ove, or (D)elete it by choosing C, M, or D If the user chooses C, ask for the destination directory and move it there If the user chooses M, ask for the destination directory and move it there If the user chooses D, delete the file. Ensure that the user enters only C, M, or D, warning them about...
Query the user for the name of a file. Open the file, read it, and count...
Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT