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 code to read the content of the text file input.txt using JAVA. For each...
● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is...
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...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is -3 10...
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...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT