Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

reate an inputfile named \"inputFile.txt\" (use \"Note Pad\") that contains the

ID: 672998 • Letter: R

Question

reate an inputfile named "inputFile.txt" (use "Note Pad") that contains the following data. Be sure that the last line of the file is empty (i.e., not even a space character). In other words, after entering the second line of data, press the <ENTER> key, save the file, then exit the editor.

5 10 6 9
23 7 13 40

Implement the following algorithm

Create & Open inputFile
Create & Open outputFile
Read first line of data into variables a, b, c, d
DOWHILE !EOF
     Create a unique formula that uses the variables a, b, c, & d. For example,
     value = (a*a) + b - c + d
     Output value into outputFile
     Read each line of data into variables a, b, c, d
ENDDO
Close inputFile
Close outputFile

Explanation / Answer

import java.io.*;

public class sample
{
    public static void main(String[] args)
    {
       System.out.println("Reading File from Java code");
       //Name of the file to read. Please note place the input file in same directory from were you are executing this program otherwise give full path of file.
       String fileName="inputFile.txt";
       try{

          //Create object of FileReader
          FileReader inputFile = new FileReader(fileName);

          //Instantiate the BufferedReader Class
          BufferedReader bufferReader = new BufferedReader(inputFile);

          //Variable to hold the one line data
          String line;
          // Instantiate the BufferedWritter Class.Please note place the output file in same directory from were you are executing this program otherwise give full path of file.

          BufferedWriter output = new BufferedWriter(new FileWriter("outputFile.txt"));

          // Read file line by line
          while ((line = bufferReader.readLine()) != null)   {
            /* line.split() methods splits the line using delimiter passed in argument. Here I passed space as delimiter because in input file every line will be written as 1 2 3 4 ie every two integer has space between them. So it will create array of type string with all the four values.*/
          
            String []array=line.split(" ");
            // array contains values in string format So first it needs to convert in to integer then only we can do calculations on them.
            int a=   Integer.parseInt(array[0]);
            int b=   Integer.parseInt(array[1]);
            int c=   Integer.parseInt(array[2]);
            int d=   Integer.parseInt(array[3]);
            int value=(a*a) + b - c + d;
          
            // It will write output value in output file
            output.write(value);
          
          
          }
          //Close the buffer reader/writer streams
          bufferReader.close();
          output.close();
       }catch(Exception e){
          System.out.println("Error while reading file line by line:" + e.getMessage());                    
       }

     }
}