Results 1 to 9 of 9
  1. #1
    kjones9687 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default 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!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,877
    Rep Power
    8

    Default Re: Using JOptionPane to mimic calculator

    What exactly are the errors you're getting? What problems are you experiencing with displaying the dialog?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

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

  4. #4
    kjones9687 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default 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."

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,877
    Rep Power
    8

    Default 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:

    Java Code:
    if(whatever){
       //something
    }
    else{
       //something else
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: Using JOptionPane to mimic calculator

    Yes, being able to run something that doesn't actually compile is something of a feat!

  7. #7
    kjones9687 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Using JOptionPane to mimic calculator

    Java 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."

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,877
    Rep Power
    8

    Default 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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: Using JOptionPane to mimic calculator

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

Similar Threads

  1. Need help with Calculator
    By Joshua4missions in forum New To Java
    Replies: 4
    Last Post: 12-10-2011, 02:44 AM
  2. RMD Calculator
    By java software in forum Java Software
    Replies: 0
    Last Post: 10-08-2011, 05:42 PM
  3. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 06:00 AM
  4. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 12:57 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •