JOptionPane Cannot Find Symbol

Gianna Caruso picture Gianna Caruso · Oct 8, 2014 · Viewed 11.8k times · Source

I am trying to create a simple calculator using JOptionPane. I have encountered compiling errors.

import javax.swing.JOptionPane; 

public class Calculator { 
  public static void main ( String args[] ) {


     double n1 = Double.parseDouble(JOptionPane.showInputDialog(" Enter first number: "));
     double n2 = Double.parseDouble(JOptionPane.showInputDialog(" Enter second number: "));
     String x = JOptionPane.showInputDialog("Enter operator: ");
     double result;

     if (x == "+"){
         result = (n1 + n2);
           JOptionPane.showMessageDialog(n1, "+", n2,"=", result); 
           }

        else if (x == "-"){
         result = (n1 - n2); 
           JOptionPane.showMessageDialog(n1,"-", n2,"=", result); 
           }


        else if (x == "/"){
            result = (n1 / n2);
            JOptionPane.showMessageDialog(n1,"/", n2,"=", result); }
        else if (n2 == 0){
                JOptionPane.showMessageDialog(null, "Cannot divide by 0"); }


        else if (x == "*"){
            result = (n1 * n2); 
            JOptionPane.showMessageDialog(n1,"*", n2,"=", result); }


        else if (x == "%"){
               result = (n1 % n2);
               JOptionPane.showMessageDialog(n1,"%", n2,"=",(n1%n2)); }
  }
}

My code brings up these compiling errors:

5 errors found: File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 14] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:14: cannot find symbol symbol : method showMessageDialog(double,java.lang.String,double,java.lang.String,double) location: class javax.swing.JOptionPane File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 19] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:19: cannot find symbol symbol : method showMessageDialog(double,java.lang.String,double,java.lang.String,double) location: class javax.swing.JOptionPane File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 25] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:25: cannot find symbol symbol : method showMessageDialog(double,java.lang.String,double,java.lang.String,double) location: class javax.swing.JOptionPane File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 32] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:32: cannot find symbol symbol : method showMessageDialog(double,java.lang.String,double,java.lang.String,double) location: class javax.swing.JOptionPane File: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [line: 37] Error: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:37: cannot find symbol symbol : method showMessageDialog(double,java.lang.String,double,java.lang.String,double) location: class javax.swing.JOptionPane

Answer

Matthew Rowlands picture Matthew Rowlands · Oct 8, 2014

I finished it off so the whole thing works:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Calculator extends JPanel {
    public static void main(String args[]) {

        JFrame f = new JFrame();

        double n1 = Double.parseDouble(JOptionPane
                .showInputDialog(" Enter first number: "));
        double n2 = Double.parseDouble(JOptionPane
                .showInputDialog(" Enter second number: "));
        String x = JOptionPane.showInputDialog("Enter operator: ");
        double result;

        if (x.equals("+")) {
            result = (n1 + n2);
            JOptionPane.showMessageDialog(f, n1 + "+" + n2 + "=" + result);
            System.out.println(x);
        }

        else if (x.equals("-")) {
            result = (n1 - n2);
            JOptionPane.showMessageDialog(f, n1 + "-" + n2 + "=" + result);
            System.out.println(x);
        }

        else if (x.equals("/")) {
            if (n2 == 0) {
                JOptionPane.showMessageDialog(null, "Cannot divide by 0");
            }else{
            result = (n1 / n2);
            JOptionPane.showMessageDialog(f, n1 + "/" + n2 + "=" + result);
            System.out.println(x);
            }
        }

        else if (x.equals("*")) {
            result = (n1 * n2);
            JOptionPane.showMessageDialog(f, n1 + "*" + n2 + "=" + result);
        }

        else if (x.equals("%")) {
            result = (n1 % n2);
            JOptionPane.showMessageDialog(f, n1 + "%" + n2 + "=" + (n1 % n2));
        }
    }
}

"==" is not the same as ".equals" -

.equals compares the value of the two objects

== compares if they are the same object in java ~