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