Question: Design a class named ReadFileLineByLine.java that reads formulas.txt and outputs the calculated values to results.txt. For instance, for each of four formulas in formulas.txt: 4

Design a class named ReadFileLineByLine.java that reads formulas.txt and outputs the calculated values to results.txt.

For instance, for each of four formulas in formulas.txt:

4 * 5

3 / 4

3 - 1

2 + 3

the following messages and formulas are displayed and calculated in the command line:

read <4 * 5>

4 * 5 = 20.0

read <3 >

3 / 4 = 0.75

read <3 - 1>

3 - 1 = 2.0

read <2 + 3>

2 + 3 = 5.0

and writes to the results.txt file the following lines:

4 * 5 = 20.0

3 / 4 = 0.75

3 - 1 = 2.0

2 + 3 = 5.0

The ReadFileLineByLine.java class should contain:

Scanner for reading the input formulas

PrintWriter for reading the outputting the results

ArrayList formula for storing the tokenized formulas, line-by-line

Some form of conditional statements for checking whether the input formula is either *,/,+, or

Parse the string values to double Write the calculated results to results.txt

Catch any exception that may exist from reading or writing files

Below is the template code for ReadFileLineByLine.java, which n:

import java.io.*;

import java.util.*;

public class ReadFileLineByLine {

public static void main(String[] args) {

String line;

try {

//open file for reading the calculated formulas "formulas.txt"

//open file for storing the calculated formulas "results.txt"

// read one line at a time while( input.hasNextLine()) {

line = input.nextLine();

System.out.println("read <" + line + ">");

// Display message to commandline //

Declare ArrayList of for storing tokenized formula from String line

double result = 0; // The variable to store result of the operation

// Determine the operator and calculate value of the result

System.out.println(formula.get(0) + ' ' + formula.get(1) + ' ' + formula.get(2) + " = " + result); // Display result to command line

// Write result to file

}

// Need to close input and output files

} catch (FileNotFoundException e) {

// Display meaningful error message

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!