Question: import java.util.Scanner; public class ExpressionCalculator { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(Paul Bowman); System.out.println(Welcome to the Calculator program.); System.out.println(You
import java.util.Scanner;
public class ExpressionCalculator {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Paul Bowman");
System.out.println("Welcome to the Calculator program.");
System.out.println("You can enter a SIMPLE mathematical expression and the program will print the value.");
System.out.println("1 + 2 is a SIMPLE expression: it has a SINGLE operator with an operand on each side.");
System.out.println("(Note the pa operator allows Calculator to be used as an accumulator by entering pa + 5");
System.out.println("or to calculate expressions with more than 2 operators, e.g. 5.7 x 5.7 followed by pi x pa");
System.out.println("Operators can be + - x or / and operands can be a number or pi or pa (for previous answer).");
System.out.println("The operands may also be preceded by a + or - sign, so -1 + -2 = -3, and -1 - -2 = 1");
System.out.println("Enter EXIT instead of an expression to terminate the program.");
Scanner scanner = new Scanner(System.in);
String leftOperand = "";
String rightOperand = "";
double leftOperandValue = 0;
double rightOperandValue= 0;
double answer = 0;
double previousAnswer = 0;
int i = 0;
int operatorOffset = 0;
char operator = ' '; // note single quotes to declare a char (and == to compare it)
while (true) {
System.out.println("");
System.out.println("Enter a simple expression or EXIT:");
String expression = scanner.nextLine().trim().toLowerCase(); // drop leading/trailing blanks
if (expression.length() == 0) continue; // ignore blanks or just ENTER
if (expression.equals("exit"))
{
scanner.close();
return; // from main() which terminates our program
}
if (expression.contains("="))
{
System.out.println("Expression cannot contain the = sign.");
continue; // back to top of while(true)
}
for (i=1; i < expression.length(); i++) // Note start at offset 1 vs 0 as discussed above.
{ // i will point to each character as it is bumped along.
if (expression.charAt(i) == '+') // note use == to compare a char field to another char field!
// FIND THE OPERATOR
// note == compare for char fields
// note scan start at 1 to skip possible leading unary operator
// so expression of -5 x -2 will find the x as the operator.
for (i=1; i < expression.length(); i++)
{
if (expression.charAt(i) == '+')
{
operator = '+';
operatorOffset = i;
break; // out of for loop
}
else if (expression.charAt(i) == '-')
{
operator = '-';
operatorOffset = i;
break; // out of for loop
}
else if (expression.charAt(i) == 'x')
{
operator = 'x';
operatorOffset = i;
break; // out of for loop
}
else if (expression.charAt(i) == '/')
{
operator = '/';
operatorOffset = i;
break; // out of for loop
}
} // end of for loop
}
leftOperand = expression.substring(0,operatorOffset).trim(); // and drop any trailing blanks
rightOperand = expression.substring(operatorOffset+1).trim(); // and drop any leading blanks
if (i == expression.length()) // scanned the WHOLE expression without doing a break;
{
System.out.println("The expression does not contain an operator (+ - x /) between two operands.");
continue; // back to top of while(true) loop
}
if (i == expression.length() - 1) // finding the operator at the end is no good either...
{
System.out.println("An operator may not end the expression.");
continue; // back to top of while(true) loop
}
if (leftOperand.equals("pa"))
leftOperandValue = previousAnswer;
else if (leftOperand.equals("pi"))
leftOperandValue = Math.PI; // PI is a constant field in the Math class
else { // assume it's a NUMBER in String form.
try {
leftOperandValue = Double.parseDouble(leftOperand);
}
catch(NumberFormatException nfe)
{
System.out.println("Left operand '" + leftOperand + "'is not numeric.");
continue; // back to top of while(true)
}
} // bottom of else
if (rightOperand.equals("pa"))
rightOperandValue = previousAnswer;
else if (rightOperand.equals("pi"))
rightOperandValue = Math.PI;
else {
try {
rightOperandValue = Double.parseDouble(rightOperand);
}
catch(NumberFormatException nfe)
{
System.out.println("Right operand '" + rightOperand + "' is not numeric.");
continue; // back to top of while(true)
}
} // bottom
if (operator == '+') answer = leftOperandValue + rightOperandValue;
else if (operator == '-') answer = leftOperandValue - rightOperandValue;
else if (operator == 'x') answer = leftOperandValue * rightOperandValue;
else if (operator == '/') answer = leftOperandValue / rightOperandValue;
previousAnswer = answer; // save for substitution for "pa" in the NEXT expression
System.out.println(expression + " = " + answer);
}
}
}
This is a java language program with the following goals:
find the operator in the entered expression
then find the left & right operands before and following the operator
then calculate the value of the expression
When I enter a simple expression, the following message shows on the console: "The expression does not contain an operator (+ - x /) between two operands."
Please help find a solution
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
