Results 1 to 8 of 8
Thread: Calculation with char
- 09-20-2010, 02:01 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
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.
The error is on x in the compute variable. Anyone know a work around for this?Java 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);
Thanks.
- 09-20-2010, 02:34 PM #2
please copy the full text of the error message and paste it here.I get an error when putting the char into a calculation
What is this statement supposed to do?Java Code:double compute = (doub_numOne x doub_numTwo);
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.
Add some operators
or write a method and pass the three variables to it as parameters.Last edited by Norm; 09-20-2010 at 02:39 PM.
- 09-20-2010, 02:39 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
Syntax error on token "x", invalid AssignmentOperator
- 09-20-2010, 02:44 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
The x is the operator. It's a char data type with values of +, -, *, /There are three variables inside the () without any operators between them.
- 09-20-2010, 02:47 PM #5
No x is not an operator. It is a variable that contains a char that represents an operator.The x is the 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 #6
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
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.
Java 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 #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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 #8
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
replaceALL(char oldChar, char newChar) method
By arson09 in forum New To JavaReplies: 0Last Post: 04-28-2010, 05:48 AM -
Help with the calculation of a variable.
By rarschach in forum New To JavaReplies: 14Last Post: 01-24-2010, 02:50 PM -
Need help with doing a calculation in Java
By John D. in forum New To JavaReplies: 6Last Post: 02-24-2009, 11:44 PM -
drawing char by char with Graphics
By diggitydoggz in forum New To JavaReplies: 5Last Post: 12-27-2008, 12:49 PM -
Problem with Calculation ....
By danny000 in forum New To JavaReplies: 1Last Post: 04-15-2008, 02:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks