Question: Hello, I'm trying to get this code to write to a CSV file (Excel spreadsheet compatible) and read from the same file. I believe I'm

Hello, I'm trying to get this code to write to a CSV file (Excel spreadsheet compatible) and read from the same file. I believe I'm close, but I keep getting the error below. I'm stuck for now. Where am I going wrong? Do you have any pointers to get me back on track? The main file is below the error. Also, if you need them, the classes are below that.

ExecuteMath.java:70: error: 'else' without 'if' } else if (choice1.equals("N")) { ^ 1 error

import java.io.FileNotFoundException; import java.util.Scanner;/* includes a class that permits use of user input in the program */

class ExecuteMath { /* This class will execute based upon the defined classes previously. It will then verify the inheritance feature of Java */

public static void main(String[] args)throws FileNotFoundException { Scanner in = new Scanner(System.in);

while (true) { System.out.print("Enter the First number >>"); /* user input */ double first = Double.parseDouble(in.nextLine()); System.out.print("Enter the Second number >>"); double second = Double.parseDouble(in.nextLine()); System.out.print("Enter operator >>"); String symbol = in.nextLine(); /* this takes the operator entered to a string object */ Math2 math2=new Math2(first,second); /* new object created with original class properties */ if (symbol.equals("+")) { System.out.println("The answer is " + math2.Add()); /* performs methods outlined in the first class */ } else if (symbol.equals("/")) { System.out.println("The answer is " + math2.Div()); /* performs methods outlined in the second class */

} else if (symbol.equals("*")) { System.out.println("The answer is " + math2.Mul());

} else { System.out.println("The answer is " + math2.Sub());

}

System.out.print("Do you want to save the results (Y/N)?"); /* user input */ String option = in.nextLine();

if (option.equals("Y")) { java.io.File file = new java.io.File("Math_Results.csv"); try { java.io.PrintWriter writeToFile = new java.io.PrintWriter(file); writeToFile.print("" + math2.Sub());

writeToFile.print(System.out.print("Enter the First number >>")); writeToFile.print(System.out.print("First: " + first)); writeToFile.print(System.out.print("Enter the Second number >>")); writeToFile.print(System.out.print("Second: " + second)); writeToFile.print(System.out.print("Operator: " + symbol)); writeToFile.print(System.out.println("The answer is " + math2.Add())); writeToFile.print(System.out.println("The answer is " + math2.Div())); writeToFile.print(System.out.println("The answer is " + math2.Mul())); writeToFile.print(System.out.println("The answer is " + math2.Sub())); writeToFile.close(); } catch (FileNotFoundException e) { //Catches FileNotFoundEception e.printStackTrace(); } else if (choice1.equals("N")) {

System.out.println("Thanks for using our system."); System.exit(0); } System.out.println(" Do you want to review the results (Y/N)?"); choice1 = input.nextLine(); if (choice1.equals("Y")) { // Create the file instance java.io.File file = new java.io.File("Math_Results.csv"); // Create a Scanner for the file Scanner newOutput = new Scanner(file); // Read the file while there is information to be read while (newOutput.hasNext()) { String first = newOutput.next(); String operator = newOutput.next(); String second = newOutput.next(); String equalsSign = newOutput.next(); String answer = newOutput.next(); // Display the information from the file to the console System.out.println("" + first + " " + second + " " + + " " + symbol + " " + math2.Add()); System.out.println("" + first + " " + second + " " + + " " + symbol + " " + math2.Div()); System.out.println("" + first + " " + second + " " + + " " + symbol + " " + math2.Mul()); System.out.println("" + first + " " + second + " " + + " " + symbol + " " + math2.Sub()); } System.out.println("Thanks for using our system."); newOutput.close(); } else if (choice1.equals("N")) { System.out.println("Thanks for using our system."); System.exit(0); } }

} } }

import java.util.Scanner;/* includes a class that permits use of user input in the program */

class Math1/* first class which introduces variables first and second */ { private double first; private double second;

public Math1 (double a , double b )/* constructors where variables are input */ { this.first= a; /* objects created as a and b */ this.second=b; }

public double getFirst() /* method gets the value of the first object */ { return first; } public void setFirst(double first)/* method sets the value of the first object */ { this.first= first; } public double getSecond() { return second; } public void setSecond(double second) { this.second = second; } public double Add()/* operation method */ { return first+second; } public double Sub() { return first-second; } }

class Math2 extends Math1 /* The first class is extended to include the new class to introduce multiplication and division */ {

Math2(double a, double b) { super(a, b); } public double Mul() { return super.getFirst() * super.getSecond();/* super is used to return to the value obtain in the original class method */ } public double Div() /* This is the method I will use for my exception which is division by zero */ { double result=0; /* New Variable to return exception message */ try /* Begins exception for user to input condition */ { result=super.getFirst() / super.getSecond(); return result; } catch (Exception e) /* Named exception for the program to catch and handle */ { e.printStackTrace(); System.out.println ("Error, Divsion by Zero"); /* Message to user to inform them the exception has been input */ result =0; System.out.println ("Try Again"); /* Directs user to be prepared to input new values */ } finally { return result; } } }

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!