Okay, start off by saying, i'm new to java. A lot of it confuses. Please bare with me as I make mistakes and won't understand things at first lol. For the following solutions please try to explain why what you did worked though as I am trying to learn with this, not actually make a useful program.
Heres the the current program, a basic calculator built to work through JOptionPane panels, I'll explain my problem at the bottom:
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) throws java.lang.NumberFormatException
{
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 = values[0];
sign = values[1];
num2a = values[2];
num1 = Double.parseDouble(num1a);
num2 = Double.parseDouble(num2a);
if (sign.equals("+")){
sum = num1 + num2;
}
else if (sign.equals("-")){
sum = num1 - num2;
}
else if (sign.equals("*")){
sum = num1 * num2;
}
else if (sign.equals("/")){
sum = num1 / num2;
}
else {
JOptionPane.showMessageDialog(null, "User error. Please follow instructions.");
}
outStr = (num1 + " " + sign + " " + num2 + " = " + sum);
JOptionPane.showMessageDialog(null, ("" + outStr));
JOptionPane.showConfirmDialog(null, "Do More Calculations?", "Calculator", JOptionPane.YES_NO_OPTION);
}
}
Okay so right now, that all works fine. The problem is... When you have that guy who really just wants to ruin my work and enter "eb a + asf sakasf" And it throws back a big error. Instead of crashing and returning my java lang exception, how can I make it say something along the lines of "User Error" as I have for the sign (If anything but +, -, /, * entered it returns that.). and then after giving the message go to the Do More Calculations? part.
The second question is... and I know i'm going to be hated for this... How do I get around java not having goto? I'm rather used to it and I don't know how to use java to return to the top of the program, perhaps some kind of loop method? I want to make it so if the user presses Yes on "JOptionPane.showConfirmDialog(null, "Do More Calculations?", "Calculator", JOptionPane.YES_NO_OPTION);" it returns to the top.
Thanks ahead of time!