Advertisements
design and implement a java program that will read a file containing numbers and compute the following statistics: the range(low,high) the average and the median
import java.io.*; class NumbersFile { public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new FileReader("numbers.txt")); LineNumberReader reader = new LineNumberReader(new FileReader("numbers.txt")); int data = reader.read(); int lineNumber=0; while(data != -1){ lineNumber = reader.getLineNumber(); } int array[]=new int[lineNumber]; int k=0; String st=""; while((st=br.readLine())!=null){ array[k]=Integer.parseInt(st); k++; } int low = array[0]; int high = array[0]; int sum=0; for (int i = 1; i < array.length; i++) { if (array[i] < low) { low = array[i]; } if (array[i]>high) { high = array[i]; } sum+=array[i]; } System.out.println("Numbers lie between "+low+" and "+high); System.out.println("Average of numbers: "+(sum/array.length)); } }