Can someone help me with this assignment? Your class with the main method will b
ID: 3651386 • Letter: C
Question
Can someone help me with this assignment?Your class with the main method will be named MathFileReader and will be in the input package. The input file will have three parameters on each line, where the values are separated by spaces. The file can also have blank lines and comment lines, which start with a #. The first line of the file will not have three parameters, but instead will have the location of a file that will be used as the output file. Here is a sample input file:
C:\output.txt
+ 3 5
- 2 4
* 100 20
# here is a division operation
/ 40 2
# the next line is intentionally blank
* 10 20
# here is another addition
+ 100 20
Your program will read the file and perform each of the operations in the file. After performing each operation, you will write the output to the file specified on the first line of the input file. The writing should be done in a different class named MathFileWriter in the output package. Here is the corresponding output file for the input file above:
3 + 5 = 8
2 - 4 = -2
100 * 20 = 2000
40 / 2 = 20
10 * 20 = 200
100 + 20 = 120
This is the code I have:
package input;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class MathFileReader {
public MathFileReader(String filename, String outputFilename) {
try
{
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("output.txt"));
String line = br.readLine();
while (line != null) {
// process the line
if (line.trim().length() == 0)
{
// ignore the blank line
}
else if (line.trim().startsWith("#")) {
// ignore the comment line
}
else
{ // we "may" have a valid line
StringTokenizer st = new StringTokenizer(line.trim(), " ");
if (st.countTokens() != 3) { // we have an invalid line
System.out.println ("INVALID LINE: " + line);
pw.println("INVALID LINE: " + line);
}
else
{ // we have a valid line
try
{
String operator = st.nextToken();
String num1 = st.nextToken();
String num2 = st.nextToken();
float fnum1 = Float.parseFloat(num1);
float fnum2 = Float.parseFloat(num2);
if (operator.equals("+"))
{
float sum = fnum1 + fnum2;
System.out.println(fnum1 + " + " + fnum2 + " = " + sum);
pw.println(fnum1 + " + " + fnum2 + " = " + sum);
}
else if (operator.equals("-")) {
float diff = fnum1 - fnum2;
System.out.println (fnum1 + " - " + fnum2 + " = " + diff);
pw.println(fnum1 + " - " + fnum2 + " = " + diff);
}
else if (operator.equals("*")) {
float prod = fnum1 * fnum2;
System.out.println (fnum1 + " * " + fnum2 + " = " + prod);
pw.println(fnum1 + " * " + fnum2 + " = " + prod);
}
else if (operator.equals("/")) {
float quot = fnum1 / fnum2;
System.out.println (fnum1 + " / " + fnum2 + " = " + quot);
pw.println(fnum1 + " / " + fnum2 + " = " + quot);
}
else if (operator.equals("%")) {
float mod = fnum1 % fnum2;
System.out.println (fnum1 + " % " + fnum2 + " = " + mod);
pw.println(fnum1 + " % " + fnum2 + " = " + mod);
}
else { // invalid operator
System.out.println ("INVALID OPERATOR: " + operator);
pw.println("INVALID OPERATOR: " + operator);
}
}
catch (NumberFormatException nfe)
{
System.out.println("NumberFormatException: " + nfe.getMessage());
pw.println("NumberFormatException: " + nfe.getMessage());
}
}
}
// read the next line
line = br.readLine();
} // ends while (line != null)
br.close();
pw.flush();
pw.close();
} catch (FileNotFoundException fnfe) {
System.out.println("FileNotFoundException: " + fnfe.getMessage());
} catch (IOException ioe) {
System.out.println("IOException: " + ioe.getMessage());
}
}
public static void usage() {
System.out.println("USAGE: java input.MathFileReader [input file] [output file]");
}
public static void main(String[] args) {
if (args.length != 2) {
usage();
return;
}
new MathFileReader(args[0], args[1]);
}
}
Explanation / Answer
//2 files, MathFIleReader and MathFileWriter //you need provide one arguments to MathFileReader, that is inputfilename //i delete the second command line parameter because you said the outputfilename //is the firstline of input file, there is no need to pass outputfilename from command //line anymore //hope it help you :D package input; import output.MathFileWriter; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.StringTokenizer; public class MathFileReader { public MathFileReader(String inputFilename) { try { MathFileWriter mfw = null; BufferedReader br = new BufferedReader(new FileReader(inputFilename)); String line = br.readLine(); boolean firstline = true; while (line != null) { // process the line if(firstline){ mfw = new MathFileWriter(line); firstline = false; line = br.readLine(); continue; } if (line.trim().length() == 0) { // ignore the blank line } else if (line.trim().startsWith("#")) { // ignore the comment line } else { // we "may" have a valid line StringTokenizer st = new StringTokenizer(line.trim(), " "); if (st.countTokens() != 3) { // we have an invalid line System.out.println("INVALID LINE: " + line); mfw.write("INVALID LINE: " + line); } else { // we have a valid line try { String operator = st.nextToken(); String num1 = st.nextToken(); String num2 = st.nextToken(); float fnum1 = Float.parseFloat(num1); float fnum2 = Float.parseFloat(num2); if (operator.equals("+")) { float sum = fnum1 + fnum2; System.out.println(fnum1 + " + " + fnum2 + " = " + sum); mfw.write(fnum1 + " + " + fnum2 + " = " + sum); } else if (operator.equals("-")) { float diff = fnum1 - fnum2; System.out.println(fnum1 + " - " + fnum2 + " = " + diff); mfw.write(fnum1 + " - " + fnum2 + " = " + diff); } else if (operator.equals("*")) { float prod = fnum1 * fnum2; System.out.println(fnum1 + " * " + fnum2 + " = " + prod); mfw.write(fnum1 + " * " + fnum2 + " = " + prod); } else if (operator.equals("/")) { float quot = fnum1 / fnum2; System.out.println(fnum1 + " / " + fnum2 + " = " + quot); mfw.write(fnum1 + " / " + fnum2 + " = " + quot); } else if (operator.equals("%")) { float mod = fnum1 % fnum2; System.out.println(fnum1 + " % " + fnum2 + " = " + mod); mfw.write(fnum1 + " % " + fnum2 + " = " + mod); } else { // invalid operator System.out.println("INVALID OPERATOR: " + operator); mfw.write("INVALID OPERATOR: " + operator); } } catch (NumberFormatException nfe) { System.out.println("NumberFormatException: " + nfe.getMessage()); mfw.write("NumberFormatException: " + nfe.getMessage()); } } } // read the next line line = br.readLine(); } // ends while (line != null) br.close(); mfw.close(); } catch (FileNotFoundException fnfe) { System.out.println("FileNotFoundException: " + fnfe.getMessage()); } catch (IOException ioe) { System.out.println("IOException: " + ioe.getMessage()); } } public static void usage() { System.out.println("USAGE: java input.MathFileReader [input file]"); } public static void main(String[] args) { if (args.length != 1) { usage(); return; } new MathFileReader(args[0]); } } //MathFileWriter.java package output; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class MathFileWriter { private PrintWriter pw; public MathFileWriter(String outputFilename){ try { pw = new PrintWriter(new FileWriter(outputFilename)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void write(String s){ pw.println(s); } public void close(){ pw.flush(); pw.close(); } }