Results 1 to 8 of 8
  1. #1
    chuckbalzer is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default 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.

    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);
    The error is on x in the compute variable. Anyone know a work around for this?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

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

    Java 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.
    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.

  3. #3
    chuckbalzer is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Syntax error on token "x", invalid AssignmentOperator

  4. #4
    chuckbalzer is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    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 +, -, *, /

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

    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.

  6. #6
    chuckbalzer is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    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);

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    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...

  8. #8
    chuckbalzer is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    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

Similar Threads

  1. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  2. Help with the calculation of a variable.
    By rarschach in forum New To Java
    Replies: 14
    Last Post: 01-24-2010, 02:50 PM
  3. Need help with doing a calculation in Java
    By John D. in forum New To Java
    Replies: 6
    Last Post: 02-24-2009, 11:44 PM
  4. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  5. Problem with Calculation ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •