# Help with five function calculator

• October 11th, 2013, 12:48 AM
catisch
Help with five function calculator
I have to create a five-function calculator. The input to my program will be a binary arithmetic expression, with the operator separated from the operands by a space like 24 * 4. The output will be the binary arithmetic expression, followed by an equal sign, surrounded by spaces, followed by the answer.

I can assume that the operands will be floating-point numbers (use doubles). The operator should be one of the following: *, /, +, -, or %. Here are important additional specifications:

-Emit an appropriate prompt (i.e. Please enter an equation with spaces between operands and operator.)

-If the operator is not one of *, /, +, -, or %, issue an appropriate error message (i.e. Sorry, '&' is not recognized).

-If operator is "/" and the second operand is zero, issue an appropriate error message.(i.e. Cannot divide by zero).

Please help! I tried over and over again and my program will either not run or it won't compile. I don't know what I'm doing wrong.

Code Java:

```  import java.util.Scanner;   public class Calculator {   public static void main(String[] args) {   Scanner in = new Scanner(System.in);   System.out.printf("Enter a binary arithmetic expression, with spaces between operands and operator. \n"); double inputOne = in.nextDouble(); double inputTwo = in.nextDouble();   char operator = 0; if (operator == '+') System.out.printf("%f + %f = %f", inputOne, inputTwo, inputOne + inputTwo); if (operator == '-') System.out.printf("%f - %f = %f", inputOne, inputTwo, inputOne - inputTwo); if (operator == '*') System.out.printf("%f * %f = %f", inputOne, inputTwo, inputOne * inputTwo); if (operator == '/')   { if (inputTwo == 0) { System.out.printf("Cannot divide by 0"); } else System.out.printf("%f / %f = %f", inputOne, inputTwo, inputOne / inputTwo);   } if (operator == '%') { System.out.printf("%f % %f = %f", inputOne, inputTwo, inputOne % inputTwo); }   }   }```
• October 11th, 2013, 06:49 AM
Norm
Re: Help with five function calculator
Where does the code read the operator that the user is supposed to type in?
• October 11th, 2013, 10:07 AM
catisch
Re: Help with five function calculator
Ok, I changed it to where I thought the code would read the operator that the user is supposed to type in, but it is still crashing?

Code Java:

```  import java.util.Scanner;   public class CheapCalculator {   public static void main(String[] args) {   Scanner in = new Scanner(System.in);   System.out.printf("Enter a binary arithmetic expression, with spaces between operands and operator. \n"); double inputOne = in.nextDouble(); double inputTwo = in.nextDouble();   String operator = in.nextLine();           if (operator.equals ('+')) System.out.printf("%f + %f = %f", inputOne, inputTwo, inputOne + inputTwo); if (operator.equals ('-')) System.out.printf("%f - %f = %f", inputOne, inputTwo, inputOne - inputTwo); if (operator.equals ('*')) System.out.printf("%f * %f = %f", inputOne, inputTwo, inputOne * inputTwo); if (operator.equals ('/'))   { if (inputTwo == 0) { System.out.printf("Cannot divide by 0"); } else System.out.printf("%f / %f = %f", inputOne, inputTwo, inputOne / inputTwo);   } if (operator.equals('%')) { System.out.printf("%f % %f = %f", inputOne, inputTwo, inputOne % inputTwo); }   }   }```
• October 11th, 2013, 10:12 AM
Norm
Re: Help with five function calculator
Quote:

it is still crashing
Please copy the full text of the error message and paste it here. It has important info about the error.
• October 11th, 2013, 10:59 AM
catisch
Re: Help with five function calculator
Enter a binary arithmetic expression, with spaces between operands and operator.
3 + 4
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at CheapCalculator.main(CheapCalculator.java:12)
• October 11th, 2013, 11:41 AM
GregBrannon
Re: Help with five function calculator
Matching up the expected input to the correct Scanner method is often more trouble than it's worth, especially if the user is uncooperative. A more bullet-proof approach is to take the whole line of input as a String and parse that into the necessary parts using String.split() or perhaps some self-designed parser with appropriate input verification/error recovery methods.
• October 11th, 2013, 12:24 PM
catisch
Re: Help with five function calculator
I'm sorry, we haven't gotten to parsing or String.split(). That is over my head.
• October 11th, 2013, 12:37 PM
Norm
Re: Help with five function calculator
Another solution would be to use a try{}catch block around the Scanner methods that read in data from the user. Put that inside a while() loop that would keep looping if the user enters bad data.

The posted code does not read the user's input in the same order as it was entered. It reads 2 numbers and then the operator. The user entered a number, the operator and another number.
• October 11th, 2013, 03:14 PM
GregBrannon
Re: Help with five function calculator
Quote:

Originally Posted by catisch
I'm sorry, we haven't gotten to parsing or String.split(). That is over my head.

Sorry. It's hard to tell what's allowed sometimes. If you're also not using try/catch blocks as Norm suggests, then ensuring the Scanner method calls match the user's entry (as Norm suggested) is your next best remedy. Still a pain in the rear and not flexible to handle an uncooperative user, but that's the breaks.
• October 12th, 2013, 09:02 AM
copeg
Re: Help with five function calculator