Basic Math Expression Java Problem

Code :

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.

Re: Basic Math Expression Java Problem

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

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

Re: Basic Math Expression Java Problem

There was a typo on here for my java code it was suppose to be:

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

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.

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.

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

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.