• 10-05-2012, 06:22 PM
Jposemato
Hi guys, Im pretty new to Java so bare with me as I make a complete fool of myself. Im trying to start out by making a simple calculator, capable of the functions plus, minus, divide, multiply. I had no trouble doing this within eclipse, however turning it into a windowed program made it a lot harder for me.

The basic problem im having is using the method:
String = JOptionPane.showInputDialog("text")

Mine looks like this:
Code:

```inStr1 = JOptionPane.showInputDialog("Please Input an Equation\n" +                                 "* = multiply, / = Divide,\n" +                                 "Include spaces and Decimals (Using Doubles!)\n" +                                 "For example: 2.0 + 4.2, Then ENTER!");```
This works all fine and dandy however I can no longer perform math with this, as it is a string. I know how to use the Double.parseDoube(), however I need it to break a line like "2.0 + 4.2" into Double num1 = 2.0; String sign = +; Double num2 = 4.2;

A good chunk of the problematic area, As you can see I tried playing around with a few things but I am completely lost at this point lol, thanks in advance.
Code:

```import java.util.*; import javax.swing.*; import java.io.*; import java.util.regex.*; public class Calculator {     static Scanner console = new Scanner(System.in);             public static void main(String[] args)         { //                System.out.println("Please Input an Equation\n" + //                                "* = multiply, / = Divide,\n" +    <--- Non-Windowed //                                "Include spaces and Decimals (Using Doubles!)\n" + //                                "For example: 2.0 + 4.2, Then ENTER!");                 double num1;                 double num2;                 double sum;                 String sign;                 String num1a;                 String num2a;                 String inStr1;                 String inStr2;                 String outStr;                 int repeats;                 num1a = "a";                 num2a = "b";                 sum = 1;                         inStr1 = JOptionPane.showInputDialog("Please Input an Equation\n" +                                 "* = multiply, / = Divide,\n" +                                 "Include spaces and Decimals (Using Doubles!)\n" +                                 "For example: 2.0 + 4.2, Then ENTER!");                 String data = inStr1;                 String[] values = data.split(" ");                 num1a = String[0];                 sign = String[1];                 num2a = String[2];                                 num1 = Double.parseDouble(num1a);                 num2 = Double.parseDouble(num2a);                 //                num1 = console.nextDouble();  <--- Old code (The none window //                sign = console.next();                                  way) //                num2 = console.nextDouble();                 if (sign.equals("+")){                         sum = num1 + num2;                 }                 if (sign.equals("-")){                         sum = num1 - num2;                 }                 if (sign.equals("*")){                         sum = num1 * num2;                 }                 if (sign.equals("/")){                         sum = num1 / num2;                 }```
I believe the part here: String[] values = data.split(" "); is the proper way of doing it, however I don't think the String[0] part is right.
• 10-05-2012, 06:58 PM
Fou-Lu
This isn't javascript, it's java. Moving to the Java forum.

Given the rules you have specified here, you can tokenize and cast the strings. Pulling from an array of String broken with a space is also sufficient.
This is almost entirely correct. The problem is you cannot pull from String[x] as String is a datatype, not a variable. You need to pull from values[x] instead.
Next to this, a try/catch should be used:
PHP Code:

```         try        {            String[] values = data.split(" ");            num1a = values[0];            sign = values[1];            num2a = values[2];            num1 = Double.parseDouble(num1a);            num2 = Double.parseDouble(num2a);            double dResult = 0.0;            if (sign.equals("+"))            {                dResult = num1 + num2;            }            else if (sign.equals("-"))            {                dResult = num1 - num2;            }            else if (sign.equals("*"))            {                dResult = num1 * num2;            }            else if (sign.equals("/"))            {                dResult = num1 / num2;            }            JOptionPane.showMessageDialog(null, "The result is: " + dResult);        }        catch (NumberFormatException ex)        {            JOptionPane.showMessageDialog(null,                    "There has been an error in input: " + ex.getMessage(),                    "Error", JOptionPane.ERROR_MESSAGE);        }        catch (ArrayIndexOutOfBoundsException ex)        {            JOptionPane.showMessageDialog(null,                    "There appears to be an error in input format (double op double expected)",                    "Error",                    JOptionPane.ERROR_MESSAGE);        }  ```
Switch can also be used on the sign, but only if it's cast to a char (Java does not allow switches on objects, and String is an object not a primitive).
• 10-09-2012, 07:19 AM
Jposemato
Hey, didn't notice I posted it in Java Script. Thought I had it in Java Forums. My bad and sorry for the inconvenience.

That was extremely helpful and I learned something new :] The calculator now works fine for everything- Aside from input errors.

Two more questions I had though if someone could answer, I'll make a new thread for it though and link it here: http://www.codingforums.com/showthre...64#post1277864