Question

Problem 1. A file that contain two columns of data are provided. You should write a...

Problem 1. A file that contain two columns of data are provided. You should write a program that calculate the average of the two number in each row, and output them into a separate file. Some row contains non-numbers or may be missing an number, and your code should use Exception to catch these errors, and output an appropriate error message.

For testing, your input data file should be:

1.2 3.5

one 4.4

2.5 three

4.1 9.9

1.9

18.5. 20.3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

ReadFileAndGetAverage.java

package fileIO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class ReadFileAndGetAverage {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String inputFileName = "input.txt";
       String outputFileName = "output.txt";

       File file = new File(inputFileName); //reading data from this file
       Scanner reader;
       System.out.println("Opening file "+inputFileName+" ....");
       String line="";
       try {
           //for writing to file
           PrintWriter outputFile = new PrintWriter(outputFileName);
           //read file using scanner
           reader = new Scanner(file);
           while(reader.hasNextLine()){
               //read line
               try{
                   line = reader.nextLine();
                   System.out.print("Processing line : "+line);
                   String datas[] = line.split(" ");
                   double num1 = Double.parseDouble(datas[0]);
                   double num2 = Double.parseDouble(datas[1]);
                   double average = (num1+ num2)/2;
                   System.out.println(" : Average : "+average);
                   outputFile.write(average+"\n");
               }catch(Exception e){
                   System.out.println(" Exception while read line "+line);
               }
           }
           outputFile.close();
           System.out.println("Data exported to file "+outputFileName+" successfully!!!");
       } catch (FileNotFoundException e) {
           System.out.println("file not found");
       }
       sc.close();
   }


}

output:

Add a comment
Know the answer?
Add Answer to:
Problem 1. A file that contain two columns of data are provided. You should write a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that loads a file called "sample.txt" in read mode, reads its content, and...

    Write a program that loads a file called "sample.txt" in read mode, reads its content, and closes the file. Use exception handling to catch any errors. 2. Compute the letter and punctuation distribution in the file. That is, output the number of a’s, the number of b’s, etc. and the number of commas, dashes, and periods. Ignore case when computing this, so ‘A’ and ‘a’ are the same letter. Use lists. 3.   Compute the number of words in the file....

  • Write a class that analyzes data you have stored in a text file. Your data represents...

    Write a class that analyzes data you have stored in a text file. Your data represents the ages of the individuals in your fictitious community. The data is stored, one per line, in a text file. For example, File name: myData.txt Contents: 45 18 6.5 3 5 sdtypo# 18 10 The number of lines in your text file must fall somewhere between 20 and 100 lines. Some of the lines must hold character strings that are not convertible to integers,...

  • Need help with code in C# Create an application that reads a file named Accounts.txt. Your application should load the file and display back the results. The application should allow the user to enter...

    Need help with code in C# Create an application that reads a file named Accounts.txt. Your application should load the file and display back the results. The application should allow the user to enter additional entries and save them back to the same file. The CSV file should contain the following fields: AccountName, InvoiceDate, DueDate, AmountDue If the CSV file contains invalid data you want to inform the user which row contained bad data. Below is an example of what...

  • Python 3:In this problem, your input file will contain a number of ciphertext and key pairs,...

    Python 3:In this problem, your input file will contain a number of ciphertext and key pairs, separated by a space. You may assume that the ciphertext contains only uppercase letters. However, it is possible that the key may be missing, and your program should handle this case. The ciphertext has been encrypted by shifting letters in the alphabet by the amount specified by the key. The word HELLO with key = 2 becomes JGNNQ. Letters should wrap around, so PIZZA...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • The input file should contain (in order): the weight (a number greater than zero and less...

    The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values. The program should also write to an output file (rather than the console, as in Horstmann). So you should also prompt the user for the name of the output file, and then print your results to an output file...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...

    For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...

  • C++ Problem: Power Plant Data. The data file power1.dat contains a power plant output in megawatts...

    C++ Problem: Power Plant Data. The data file power1.dat contains a power plant output in megawatts over a period of 10 weeks. Each row of data contains 7 floating-point numbers that represent 1 week's data. In developing the following programs, use symbolic constants NROWS and NCOLS to represent the number of rows and columns in the array used to store the data. Write a function to compute and return the average value in a two-dimensional vector of type double. Assume...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT