Question

In: Computer Science

In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...

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

Solutions

Expert Solution

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=====


Related Solutions

C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their...
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their total and average. In addition, the total will be multiplied by an integer that is supplied by the user. ** IMPORTANT: The numbers are included in your starter code as a separate file (numbers.txt). Don't touch that file! All of your code should be placed in code.cpp, as usual. ** Input: After all numbers are read, what number would you like to multiply the...
Write a program that reads a text file and reports the total count of words of...
Write a program that reads a text file and reports the total count of words of each length. A word is defined as any contiguous set of alphanumeric characters, including symbols. For example, in the current sentence there are 10 words. The filename should be given at the command line as an argument. The file should be read one word at a time. A count should be kept for how many words have a given length. For example, the word...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
In java, Write a program that reads a data file containing final exam scores for a...
In java, Write a program that reads a data file containing final exam scores for a class and determines the number of passing scores (those greater than 60) and the number of failing scores (those less than 60). The average score as well as the range of scores should also be determined. The program should request the name of the data file from the end user. The file may contain any number of scores. Using a loop, read and process...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT