1

I am adding this a code this give below error:

// Printing in a dialog box import javax.swing.JOptionPane;

public class ExampleWorking {
    public static void main(String[] args) {
        String  firstNumber,
                secondNumber,
                opp;
        int number1,
            number2,
            results;
        boolean use;
        firstNumber = JOptionPane.showInputDialog("Enter First integer");
        secondNumber = JOptionPane.showInputDialog("Enter Second integer");
        opp = JOptionPane.showInputDialog("Enter Method");
        number1 = Integer.parseInt(firstNumber);
        number2 = Integer.parseInt(secondNumber);
        use = boolean.parseBoolean(opp);
        if (use="+") {
            results= number1 + number2;
        }
        else{
            if(use="-"){
                results=number1 - number2;
            }
            else{
                if (use="*"){
                    results=number1 * number2;
                } else {
                    if (use="/") {
                        results=number1/number2;
                    } else {
                        System.out.println("Hello World");
                    }
                }
            }
        }
        JOptionPane.showMessageDialog(null, "The Results is " + results , "Results", JOptionPane.PLAIN_MESSAGE);
        System.exit(0);
    }
}       

Error

ExampleWorking1.java:21: error: incompatible types: String cannot be converted to char

opp = JOptionPane.showInputDialog("Enter Method");

1 error

Community
  • 1
  • 1

1 Answers1

0

Problem with your code is that you have multiple compilation errors, and although none of them corresponds to the error message you've added, fixing them will solve your problem.

  • Your variable use is of type boolean, but you're trying to assign a String to it, which are incompatible types, and also is not needed in your case. You don't need a boolean variable for storing a sign.

  • In all of your if statements, you're using operator = , which is used for assigning value to variable and not for comparison (for more information about assigning values, see this tutorial). If you want to compare values, you should use == for primitive data types, and equals method for String - you can see why here.

Once you fix incorrect type of use variable and use proper comparison for your data type instead of re-assigning variable value inside of if condition, your code will be working fine.

ailav
  • 186
  • 8