In: Computer Science
In java
Write a program called FileProcessor.java that reads the file numbers.txt, and:
Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
Hi,
Please find the code below:
Note:
1. We are assuming that the number.txt file will only consist of number(or white spaces).
2. If in case program won't work, please place a comment. I will try to get the answer as soon as possible.
====================Code====================
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Arrays;
import java.util.Scanner; // Import the Scanner class to read text files
public class FileProcessor {
//Wrote this method separately to calculate the standard deviation
//This method accept double array to calculate the standard deviation
//It will return the calculated standard deviation
public static double standardDeviation(double numArray[])
{
double sum = 0.0, standardDeviation = 0.0;
int length = numArray.length;
for(double num : numArray) {
sum += num;
}
double mean = sum/length;
for(double num: numArray) {
standardDeviation += Math.pow(num - mean, 2);
}
return Math.sqrt(standardDeviation/length);
}
//Main method
public static void main(String[] args) {
//Declaring variables which can be used
int[] arrayOfNumbers;
String data;
double totalCount;
double averageOfNumbers;
try {
//Reading number.text file which is place inside my project directory
//Writing this inside try block because it may throw an exception if file not found
File myObj = new File("number.txt");
Scanner myReader = new Scanner(myObj);
totalCount=0;
averageOfNumbers=0;
//Reading line by line (We are assuming the file only consists of numbers)
while (myReader.hasNextLine()) {
data = myReader.nextLine();
//replacing all the white spaces
data=data.replaceAll("\\s", "");
//Count the total number of numbers in file
System.out.println("Total number of numbers ="+data.length());
arrayOfNumbers=new int[data.length()];
/*Splitting the string to convert it into string array,
* this will help us to parse it into integer array in next step.
*
*/
String[] ary = data.split("");
//Assigning all the string array data to integer array by parsing it.
//It will be easier to work on int array of number than string array pf numbers
for(int i=0;i<arrayOfNumbers.length;i++) {
arrayOfNumbers[i]=Integer.parseInt(ary[i]);
}
//Calculating the sum of all numbers
for(int i=0;i<arrayOfNumbers.length;i++) {
totalCount=totalCount+arrayOfNumbers[i];
}
System.out.println("Total sum of numbers= "+totalCount);
//Calculating the average of all the numbers
averageOfNumbers=totalCount/data.length();
System.out.println("Average of all the numbre are="+averageOfNumbers);
//Sorting int array to get the smallest number and largest number in the file
Arrays.sort(arrayOfNumbers);
System.out.println("Smallest number= "+arrayOfNumbers[0]);
System.out.println("Largest value in numbers is= "+arrayOfNumbers[data.length()-1]);
//Converting the int array of number to double so that we can feed this double array to standard deviation function
double[] arrayOfNumbersInDoubleToCalculateStandardDeviation = new double[arrayOfNumbers.length];
for(int i=0; i<arrayOfNumbers.length; i++) {
arrayOfNumbersInDoubleToCalculateStandardDeviation[i] = arrayOfNumbers[i];
}
System.out.println("Standard deviation is=" +standardDeviation(arrayOfNumbersInDoubleToCalculateStandardDeviation));
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
======================Output================
===================If you find this helpful then please consider upvoting me====Thanks=====