Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user for the file name. Assume the file contains a

ID: 3709384 • Letter: W

Question

Write a program that asks the user for the file name. Assume the file contains a series of numbers, each written on a separte line. The program should read the contexts of the file into an array and then display the following data:

* The lowest number in the array

* The highest number in the array

* The total of the numbers in the array

* The average of the numbers in the array

if you have downloaded this book's source code, you will find the file named numbers.txt in the Chapter 07 folder. You can use the file to test the program.

I need help with this program to be written with VECTORS instead of ARRAYS.

Thank you

Explanation / Answer

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.util.Scanner;

import java.util.Vector;

public class NumberExtractor {

public static void main(String[] args) {

String fileName;

Scanner input = new Scanner(System.in);

System.out.print("Enter file name >> ");

fileName = input.nextLine();

long max = Long.MIN_VALUE, min = Long.MAX_VALUE, total = 0, count = 0;

Vector<Long> numbers = new Vector<>();

String line;

BufferedReader br;

try {

br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName).getAbsolutePath())));

while ((line = br.readLine()) != null) {

long current = Long.parseLong(line);

numbers.add(current);

}

for(int i = 0; i<numbers.size(); i++) {

long current = numbers.get(i);

// the vector wasn't needed. this code could have been written in above while loop

if(current > max) max = current;

if(current < min) min = current;

total += current;

count++;

}

System.out.println("Maximum = " + max);

System.out.println("Minimum = " + min);

System.out.println("Total numbers = " + count);

System.out.println("Average = " + (long)(total/count));

} catch (IOException e) {

e.printStackTrace();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote