Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Basic Math Expression Java Problem

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Basic Math Expression Java Problem

    import java.util.*;
    import java.text.*;
    import javax.swing.*;
    class Lab4Part2{
       public static void main (String[] args) {
    		String ex1;
    		Double num, F;
    		final int a=0, b=0;
    		JFrame myWindow=new JFrame();
    		myWindow.setSize(500,400);
    		myWindow.setTitle("Calculator");
    		myWindow.setVisible(true);
    		myWindow.setLocation(600,200);
    		ex1=JOptionPane.showInputDialog(myWindow, "Enter an expression");
    	        F=Double.parseDouble(ex1);
    		num=(a+B);
    		JOptionPane.showMessageDialog(myWindow, "The sum is "+num);
    	}
    }
    I need some help I keep getting an error when I try to run it on JGrasp. I have no clue what I'm doing wrong and for that matter I don't know what I have to do next if I am doing something wrong or not. However I'm trying to create a program where there are one input dialog that asking for the expression in "a+b" form like I would have to input "1+2" and then the second dialog would be a message giving the answer to that 1+2 which would show the message of 3. Thanks for helping.

    PS. I believe the Double.parseDouble is a wrong method in solving this issue and I believe I need to stack but I don't know how.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Basic Math Expression Java Problem

    For future reference, it helps to post the full error you are getting. From what you posted however:

    num=(a+B);

    I see 'b' defined, but not 'B'. Next issue is that you cannot parse an expression (such as '1+3') into a double. You will need some other means to either retrieve the values from the user, or parse the expression (eg search for numbers and operators in the String retrieved from the user).

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Math Expression Java Problem

    There was a typo on here for my java code it was suppose to be:
    num=(a+b);
    And I don't quite understand how to do what you told me I should because I am a freshman in college and is only taking beginners object oriented programming with java. Is there a way you could explain? Thanks

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Basic Math Expression Java Problem

    My understanding is you want to be able to evaluate a math expression that is represented as a String. : for example "1+3". Your first choice is to give the user several windows to enter a) the numbers and b) the math operator (this would make 3 JOPptionPane's). Your second options is to present one window where the user enters the expression, in which case you need a way to be able to extract out the numbers (1 and 3) and the math expression (+) from the returned string before doing the math. You can do so using the charAt method of string, testing each to see if it is a digit or operator and taking the appropriate action either way.

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Basic Math Expression Java Problem

    Copeg is saying that you can not convert an expression to a double. Example: 1+2 cannot be parsed into 3 using a double. You have to use a String and search for an operator (+, -, *, or /) and then search for the numbers. His post was pretty clear, so you shouldn't have any problems now.

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Math Expression Java Problem

    Also there is an open source java library for evaluating mathematical expressions at http://zee.dev.java.net

    You could use this code to write out "3.0":

    String input = "1+2";
    double result = EquationProcessor.evaluateExpression(input);
    System.out.println(result);

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Math Expression Java Problem

    There's also exp4j an implementation of Dijkstra's Shunting Yard, it's able to cope with most requirements for expression evaluation.

    http://projects.congrace.de/exp4j

    The library is only about 25kb in size and It's been released under the Apache License 2, so you are able to redistribute it.

    Hope that helped.

Similar Threads

  1. Basic Math Expression Java Problem
    By andyluvskrissy in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 02:46 PM
  2. Math Problem
    By kidsforsale in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 14th, 2010, 04:09 PM
  3. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM
  4. Magic square class (modular math problem)
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2010, 10:56 AM
  5. switch and math problem issues
    By emartino in forum Java Theory & Questions
    Replies: 1
    Last Post: January 19th, 2010, 05:34 PM