Question

Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in...

Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in a few lines. In a single file view to create a list of these numbers and find the arithmetic mean of the list elements. The resulting value is recorded in a text file OUTPUT.TXT.

Homework Answers

Answer #1

Code : AverageWriter.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class AverageWriter {

   public static void main(String[] args) {
       AverageWriter t = new AverageWriter();
       // Used to find average line by line and write in different line of output file
//       t.findAverageByLineWriteTo("src/INPUT.TXT","src/OUTPUT.TXT");
       // Used to find whole avarage and write in output file
       t.findAverageWriteTo("src/INPUT.TXT","src/OUTPUT.TXT");
   }
  
   public void findAverageWriteTo(String inputFile, String outputFile) {
       BufferedReader br = null;
       try {
           br = new BufferedReader(new FileReader(inputFile));
       StringBuilder sb = new StringBuilder();
       String line = br.readLine();
       int sum = 0;
       int totalNum = 0;
       while (line != null) {
       sb.append(line);
       sb.append(System.lineSeparator());
       if(line != null) {
           String[] strArray = line.split(" ");
           int[] nums = new int[strArray.length];
           for (int i=0; i<strArray.length; i++) {
                       nums[i] = Integer.parseInt(strArray[i]);
                       sum += nums[i];
                   }
           totalNum += nums.length;
             
       }
       line = null;
       line = br.readLine();
       }
       float average = sum/totalNum;
       AverageWriter t = new AverageWriter();
   t.writeToFile(outputFile, String.valueOf(average));
       System.out.println(average);
       String everything = sb.toString();
       System.out.println(everything);
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
       try {
               br.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }

   public void findAverageByLineWriteTo(String inputFile, String outputFile) {
       BufferedReader br = null;
       try {
           br = new BufferedReader(new FileReader(inputFile));
       StringBuilder sb = new StringBuilder();
       String line = br.readLine();

       while (line != null) {
       sb.append(line);
       sb.append(System.lineSeparator());
       if(line != null) {
           String[] strArray = line.split(" ");
           int[] nums = new int[strArray.length];
           int sum = 0;
           for (int i=0; i<strArray.length; i++) {
                       nums[i] = Integer.parseInt(strArray[i]);
                       sum += nums[i];
                   }
           float average = sum/nums.length;
           AverageWriter t = new AverageWriter();
           t.appendToFile(outputFile, String.valueOf(average));
               System.out.println(average);
       }
       line = null;
       line = br.readLine();
       }
       String everything = sb.toString();
       System.out.println(everything);
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
       try {
               br.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }
  
   public void writeToFile(String fileName, String content) {
       BufferedWriter bw = null;
       FileWriter fw = null;
       try {
           fw = new FileWriter(fileName);
           bw = new BufferedWriter(fw);
           bw.write(content);
           System.out.println("Done");
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (bw != null)
                   bw.close();
               if (fw != null)
                   fw.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }
   }
  
   public void appendToFile(String fileName, String content) {
       BufferedWriter bw = null;
       FileWriter fw = null;
       try {
           fw = new FileWriter(fileName, true);
           bw = new BufferedWriter(fw);
           bw.write(content);
           bw.newLine();
           System.out.println("Done");

       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (bw != null)
                   bw.close();
               if (fw != null)
                   fw.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }
   }

}

Comment : Keep the INPUT.TXT in right path and execute the file in java.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that reads a file named input.txt and writes a file that contains the...
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
1) Write a program that stores the first 50 integers to a text file named numbers.txt....
1) Write a program that stores the first 50 integers to a text file named numbers.txt. Each number should appear on a line all by itself.
How to write a C++ program. Additive persistence is a property of the sum of the...
How to write a C++ program. Additive persistence is a property of the sum of the digits of an integer. The sum of the digits is found, and then the summation of digits is performed creating a new sum. This process repeats until a single integer digit is reached. Consider the following example: 1. The beginning integer is 1234 2. Sum its digits is 1+2+3+4 = 10 3. The integer is now 10 4. The sum of its digits is...
In C++ Write a simple program to generate random integers between X and Y and store...
In C++ Write a simple program to generate random integers between X and Y and store them in a file, one number per line. The user should input the number of elements to generate and the boundary numbers X and Y (e.g. the inputs 100, 0, 999 mean your program should generate a list of 100 integers with values between 0 and 999, inclusive).
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
1.      Create 100 text files automatically; in each file write a random number from 1 to 10....
1.      Create 100 text files automatically; in each file write a random number from 1 to 10. Use outputstreams (fileoutputstream, buffredwriter….) 2.      Read the content of the 100 files and combine them into a 1 single file. 3.      Write java code to do the following: a.      To write the following text into a text file EEEESAAA@23SDCFSAWERF%WASDFGHWERTRQW b.      Read the file using a java program c.      Find how many D’s in the file d.      Extract the text between the @ and the # 1. Create 100 text files...
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
Attached is a text file full of names. Write a program that prompts the user to...
Attached is a text file full of names. Write a program that prompts the user to type in a name. If the name appears in the list of names, the program prints a message indicating the name was found. If the name entered by the user is not in the database, the program prints a different message indicating the name was not found. The program will continue prompting the user and searching for names until the user enters "quit". The...
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
Consider the following list contained in file named input.txt: using java program Khaled 65 78 56...
Consider the following list contained in file named input.txt: using java program Khaled 65 78 56 89 -999 Kamal 37 55 44 22 18 56 26 -999 Ali 66 33 87 66 87 25 39 78 -999 Taher 12 67 -999 Write a program to read this information and Output the name and sum for each person. Note: the number of rows may vary. Assum -999 as sentenital value