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

Can Somebody Please, Please hep me with this (Programming in Java): Use an input

ID: 3766209 • Letter: C

Question

Can Somebody Please, Please hep me with this (Programming in Java):

Use an input file “MyInput.csv” with instructions to perform math operation

Here is a sample

2,+

6,+

9,-

10,*

=

4, /

5, *

6, -

=

The “=” indicates the end of the set

The results should be something like this for the first set in the output file “MyOutput.CSV”

Add         2     total         2

Add           6      total          8

Subtract   9     total         -1

Multiply 10      total        -10

Number of elements = 4, Total = -10, Average = -2.5

********************************************

Divide        4      total 0

Multiply      5      total 0

Subtract     6      total -6

Number of elements=3, Total = -6, Average = -2

********************************************

Create a file with at least 10 sets.

Explanation / Answer

Here is the code you want. I have commented in the code where ever necessary for your understandin

import java.io.*;

public class CSVRead2 {
public static void main(String args[]) {

String operator[] = new String[1];
int number[] = new int[1];
int total = 0;
int i=0;

// The name of the file to open.
String inputFile = "Myinput.csv";

// This will reference one line at a time
String line = null;

try { // start monitoring code for Exceptions
// FileReader reads text files in the default encoding.
FileReader read = new FileReader("Myinput.csv");
// Always wrap FileReader in BufferedReader.
BufferedReader buffRead = new BufferedReader(read);

// Assume default encoding.
FileWriter write = new FileWriter("Myoutput.csv", true); // true for append
// Always wrap FileWriter in BufferedWriter.
BufferedWriter buffWrite = new BufferedWriter(write);

// The name of the file to open.
String outputFile = "Myoutput.csv";

while ((line = buffRead.readLine()) != null) {
String[] value = line.split(",");
operator[i] = value[1];
number[i] = (Integer.parseInt(value[0])); // Change from a String to an integer.
// Determine the operator and do the math operation and write to the output file.
if (operator[i].equals("+")) { // if statement for addition operator
total = total + number[i];
buffWrite.write("Add " + number[i] + " total " + total);
buffWrite.newLine();
}else if (operator[i].equals("-")) { // if statement for subtraction operator
total = total - number[i];
buffWrite.write("Subtract " + number[i] + " total " + total);
buffWrite.newLine();
}
else if (operator[i].equals("*")) { // if statement for multiplication operator
total = total + number[i];
buffWrite.write("Multiply " + number[i] + " total " + total);
buffWrite.newLine();
}
else if (operator[i].equals("/")) { // if statement for division operator
total = total + number[i];
buffWrite.write("Divide " + number[i] + " total " + total);
buffWrite.newLine();
}
else if (operator[i].equals("=")) { // if statement for equals operator
buffWrite.newLine();
}

}
buffWrite.flush();   
// closing BufferedReader and BufferedWriter
buffRead.close();
buffWrite.close();
}
catch(FileNotFoundException ex) { // will catch if file is not found
System.out.println( "Unable to open file '" + inputFile + "'");
}
catch(IOException ex) // catches read and write errors
{
ex.printStackTrace(); // will print read or write error
}
}
}

Myinput.csv ( no empty lines in the file)

2,+

3,+

9,-

Myoutput.csv

Add 2 total 2

Add 3 total 5

Subtract 9 total -4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote