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
Here is what I have so far: I need the code for the comments that say "Determine the operator and calculate value of the result" and "Write result to file"
If you could also double check my code as well I would greatly appreciate it!
import java.io.*; import java.util.*; public class ReadFileLineByLine { public static void main(String[] args) throws FileNotFoundException { String line; Scanner input = null; PrintWriter output = null;
try { //open file for reading the calculated formulas "formulas.txt" input = new Scanner(new File("C:\\formulas.txt")); //open file for storing the calculated formulas "results.txt" output = new PrintWriter(new File("C:\ esults.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 StringTokenizer defaultTokenizer = new StringTokenizer(line); String[] arr = new String[3]; arr[0] = defaultTokenizer.nextToken(); arr[1] = defaultTokenizer.nextToken(); arr[2] = defaultTokenizer.nextToken();
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 input.close(); output.close(); } catch (FileNotFoundException e) { // Display meaningful error message System.out.println("File is missing."); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started