# Calculation with char

• 09-20-2010, 02:01 PM
chuckbalzer
Calculation with char
Hey Everyone,
I'm a little stuck and looking for a little help.

For a class assignment, I have a dialogue box input for a simple calculator. eg 5*8

I'm casting the 5 and 8 to double and the "*" to a char.

I get an error when putting the char into a calculation and I'm not sure how else to do it.

Code:

```                // Convert numOne from a string to double                 double doub_numOne = Double.parseDouble(numOne);                                 // Convert operand from a string to a char                 int ascii =  operand.charAt(0);                 char x = (char)ascii;                                 // Convert numTwo from a string to double                 double doub_numTwo = Double.parseDouble(numTwo);                                 // Perform the computation                 double compute = (doub_numOne x doub_numTwo);                                 // Output to the screen                 System.out.printf("%.02f %c %.02f = %.02f", doub_numOne, x, doub_numTwo, compute);```
The error is on x in the compute variable. Anyone know a work around for this?

Thanks.
• 09-20-2010, 02:34 PM
Norm
Quote:

I get an error when putting the char into a calculation
please copy the full text of the error message and paste it here.

Code:

`double compute = (doub_numOne x doub_numTwo);`
What is this statement supposed to do?
There are three variables inside the () without any operators between them. The compiler is confused and needs instructions on how to generate code from that.
or write a method and pass the three variables to it as parameters.
• 09-20-2010, 02:39 PM
chuckbalzer
Syntax error on token "x", invalid AssignmentOperator
• 09-20-2010, 02:44 PM
chuckbalzer
Quote:

There are three variables inside the () without any operators between them.
The x is the operator. It's a char data type with values of +, -, *, /
• 09-20-2010, 02:47 PM
Norm
Quote:

The x is the operator
No x is not an operator. It is a variable that contains a char that represents an operator.
You need to write a method that takes 3 parameters and returns the result of treating the middle parameter as an operator.
• 09-20-2010, 02:58 PM
chuckbalzer
I see what your saying but this was a class assignment and we havn't made it to methods yet.

Maybe I'm approaching this the wrong way. The assignment was to take an equation from a dialogue box and output the result.

My thought was to take apart the input string into it's two numbers and operator and perform the calculation.

Is there a way to do this without a method? Below is the full code. Thanks for your help.

Code:

```                // Show Input Dialog box                 String str = JOptionPane.showInputDialog("Enter an Equation (Without Spaces). e.g. 5+5");                                 // Find where the operand is in the string                 int times = str.indexOf ("*");                 int devide = str.indexOf("/");                 int add = str.indexOf("+");                 int subtract = str.indexOf("-");                                 // Declare and Initialize computation variables                 String numOne = null;                 String operand = null;                 String numTwo = null;                                 // Insert a space before and after the operand so the loop can differentiate between the words                 if (times > 0) {                         str = new StringBuffer(str).insert(times, " ").toString();                         str = new StringBuffer(str).insert(times + 2, " ").toString();                 }else if (devide > 0) {                         str = new StringBuffer(str).insert(devide, " ").toString();                         str = new StringBuffer(str).insert(devide + 2, " ").toString();                        }else if (add > 0) {                         str = new StringBuffer(str).insert(add, " ").toString();                         str = new StringBuffer(str).insert(add + 2, " ").toString();                        }else if (subtract > 0) {                         str = new StringBuffer(str).insert(subtract, " ").toString();                         str = new StringBuffer(str).insert(subtract + 2, " ").toString();                        }                                 // Extract each word in the string and assign to a variable                 String[] words = str.split (" ");                 for (int i = 0; i < words.length; i++)        {                         if (i == 0) {                                 numOne = words[i];                         }if (i == 1) {                                 operand = words[i];                         }if (i == 2) {                                 numTwo = words[i];                         }                 }                                 // Convert numOne from a string to double                 double doub_numOne = Double.parseDouble(numOne);                                 // Convert operand from a string to a char                 int ascii =  operand.charAt(0);                 char x = (char)ascii;                                 // Convert numTwo from a string to double                 double doub_numTwo = Double.parseDouble(numTwo);                                 // Perform the computation                 double compute = (doub_numOne x doub_numTwo);                                 // Output to the screen                 System.out.printf("%.02f %c %.02f = %.02f", doub_numOne, x, doub_numTwo, compute);```
• 09-20-2010, 04:05 PM
Tolls
Assuming your first technique (splitting into 2 numbers and an operator) works for getting the values, can't you if/else on the char?

Or use switch/case if you've learned that bit yet...
• 09-20-2010, 05:29 PM
chuckbalzer
Thats what I ended up doing. I just didn't know if there was a datatype I could use that would allow me to perform the calculation.

Thanks again for your help