Question: In this tutorial you will learn how compare read from a text file into an ArrayList then use those elements to fill a two dimensional
In this tutorial you will learn how compare read from a text file into an ArrayList then use those elements to fill a two dimensional array.
On blackboard, download the skeleton code for the FileCalculator.java class. Create a sample project in your chosen IDE for this tutorial. The commented code in the files should provide you with instructions to fulfill the tasks for each. Add both files to your src directory in the IDE project files. Fulfill the tasks asked in the comments and run the code.
In this task, you are to read from a text file and use an array list to read data that you will use in a specified calculation. After the calculation is made, you are to write the results to an output file.
USE THIS OUTLINE:
import java.io.*; import java.util.*; import javax.script.*; public class FileCalculator { String operator; ArrayList lines = new ArrayList(); ArrayList results = new ArrayList(); int [][] numbers; String inputPath; public FileCalculator(String path, String operator){ // populate object variables } public void readLineByLine(BufferedReader reader)throws IOException{ // create input string to check for null input from buffered reader // while the input from the buffered reader is not null // read the next line, add to lines // populate the numbers array with empty array of size lines.size(),2 } public void convertToArray(){ // go through each line from lines and split the string into two ints // populate the numbers array } public void calculate()throws ScriptException{ // if the operator is (*,+,-) // if operator is + // for each row in numbers, evaluate using + // add to results // else if operator is - // for each row in numbers, evaluate using - // add to results // else if operator is * // for each row in numbers, evaluate using * // add to results // else, the operator is invalid, print that the operator is invalid } public void writeToFile()throws IOException{ String newFile = "answers.txt"; // create file from input path String parentDirectory = this.inputPath.substring(0,file.getAbsolutePath().lastIndexOf(File.separator)); String newPath = parentDirectory + "/" + newFile; System.out.println("Written to: " + newPath); // create BufferedWriter from newPath // for each result, write to file, append newline on each write // close writer } public static void main(String [] args)throws IOException{ // correct path for your pc String path = "/home/neil/Documents/object-oriented-programming/Tutorial 9/solution/numbers.txt"; try{ // create FileCalculator with path and operator // create FileReader from path // create BufferedReader from FileReader // execute line by line read // convert lines to array // calculate results // write to file }catch(FileNotFoundException ex) { System.out.println("Unable to open file '" + path + "'"); }catch(ScriptException ex) { System.out.println("Error executing operation"); } } }
numbers.txt should have this on it:
4,5 7,8 3,2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
