Using JOptionPane to mimic calculator
I am attempting to write a code using if-else statements that asks for a user's input of an equation using +, -, *, /, or %. I am supposed to produce a pop-up window with the answer unless they enter some other operator, in which case I'm supposed to produce an error message. So far, this is what I have:
import java.util.*;
import javax.swing.*;
public class Calculator
{
public static void main(String[] arg)
{
String inputString;
double operand1, operand2;
String num1, num2, operator;
int position, position2;
inputString = JOptionPane.showInputDialog("This calculator supports the functions +, -, *, / and %."
+ "\n\n\nEnter your calculation separated by spaces and press ok.");
position = inputString.indexOf(" ");
num1 = inputString.substring(0, position);
operand1 = Double.parseDouble(num1);
position2 = inputString.lastIndexOf(" ");
operator = inputString.substring(position, position2);
num2 = inputString.substring(position2);
operand2 = Double.parseDouble(num2);
double result;
if (operator.equals("+"))
result = operand1 + operand2;
else if (operator.equals("-"))
result = operand1 - operand2;
else if (operator.equals("*"))
result = operand1 * operand2;
else if (operator.equals("/"))
result = operand1 / operand2;
else if (operator.equals("%"))
result = operand1 % operand2;
JOptionPane.showMessageDialog(null,"Your results are " + result);
else
{
JOptionPane.showMessageDialog(null, "Inappropriate Input");
}
}
// TODO Auto-generated method stub
private static double operand2(double result) {
// TODO Auto-generated method stub
return 0;
}
}
I am getting a few errors and am unsure how to insert the pop-up box with the result. Any help would be greatly appreciated!
Re: Using JOptionPane to mimic calculator
What exactly are the errors you're getting? What problems are you experiencing with displaying the dialog?
Re: Using JOptionPane to mimic calculator
Please put your code in [code][/code]tags so that it retains its formatting.
Unformatted code is hard to follow.
Also you should tell us what your errors are and what lines they are happening on.
Re: Using JOptionPane to mimic calculator
When I try to run it, even when I use one of the supported operators, I get the "Inappropriate Input" pop-up.
I have an error at the final "else" statement, which says "Syntax error on token "else," delete this token.
I also thought I was supposed to end the code with System.exit(0); but every time I insert it, I get an error saying "unreachable code."
Re: Using JOptionPane to mimic calculator
I'm not really sure how you can get compilation errors but still run your code to get the popup. In addition to Tolls's advice on using CODE tags, I suggest wrapping each if/else statement body into properly indented {curly brackets}, such as:
Code:
if(whatever){
//something
}
else{
//something else
}
Re: Using JOptionPane to mimic calculator
Yes, being able to run something that doesn't actually compile is something of a feat!
Re: Using JOptionPane to mimic calculator
Code:
import java.util.*;
import javax.swing.*;
public class Calculator
{
public static void main(String[] arg)
{
String inputString;
double operand1, operand2;
String num1, num2, operator;
int position, position2;
inputString = JOptionPane.showInputDialog("This calculator supports the functions +, -, *, / and %."
+ "\n\n\nEnter your calculation separated by spaces and press ok.");
position = inputString.indexOf(" ");
num1 = inputString.substring(0, position - 1);
operand1 = Double.parseDouble(num1);
position2 = inputString.lastIndexOf(" ");
operator = inputString.substring(position, position2);
num2 = inputString.substring(position2 + 1);
operand2 = Double.parseDouble(num2);
double result;
if (operator.equals("+"))
{
result = operand1 + operand2;
}
else if (operator.equals("-"))
{
result = operand1 - operand2;
}
else if (operator.equals("*"))
{
result = operand1 * operand2;
}
else if (operator.equals("/"))
{
result = operand1 / operand2;
{
if(operand2 == 0)
JOptionPane.showMessageDialog(null, "Cannot divide by 0");
}
}
else if (operator.equals("%"))
{
result = operand1 % operand2;
}
JOptionPane.showMessageDialog(null, "Your result is" + result);
}
else
{
JOptionPane.showMessageDialog(null, "Inappropriate Input");
}
// TODO Auto-generated method stub
{
// TODO Auto-generated method stub
System.exit(0);
}
}
This is the first code I've ever attempted so I'm sorry if I'm not great at posing these questions. I made some changes after suggestions from others, but still have an error on my "else" statement: "syntax error on token "else," delete this token." I tried to delete the else statement just to see how the program ran without it, and I got an error on the line right above it in the JOptionPane.showMessageDialog command that said "the local variable result may not have been initialized."
Re: Using JOptionPane to mimic calculator
Well, you have a couple problems. First off, I see two closing parentheses before your else. What does each close? With proper indentation, the opening and closing parentheses would all line up.
Secondly, the error is pretty self-explanatory. After all your else if statements, you use the result variable. What if none of the if/else if statements evaluate to true? What value will result hold?
Re: Using JOptionPane to mimic calculator
Code:
else if (operator.equals("%"))
{
result = operand1 % operand2;
}
JOptionPane.showMessageDialog(null, "Your result is" + result);
}
else
That JOptionPane call is outside the if/esleif collection so the compiler thinks that lot is finished, so when it encounters the 'else' it has nothing to tie it to.
I'm guessing that should be after the 'else' block.