-1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class calc implements ActionListener
{
    JFrame f;
    JPanel p;
    JTextField jt1,jt2,jt3;
    JButton j1,j2;
    static double a=0,b=0,result=0;
    static int operator=0;

  calc() 
  {
      f = new JFrame();
      p = new JPanel();
      jt1 = new JTextField(20);
      jt2 = new JTextField(20);
      j1 = new JButton("+");
      j2 = new JButton("-");
      jt3 = new JTextField();

      f.add(p);
      p.add(jt1);
      p.add(jt2);
      p.add(j1);
      p.add(j2);
      j1.addActionListener(this);
      j2.addActionListener(this);

      f.setVisible(true);
      f.pack();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public void actionPerformed(ActionEvent ae)
  {
      if(ae.getSource()=="+")
      {
          a=Double.parseDouble(jt1.getText());
          operator = 1;
          b=Double.parseDouble(jt2.getText());
          switch(operator)
          {
              case 1: result=(a+b);

          }

          jt3.setText(result);

      }
  }
  public static void main(String [] args)
  {
      calc obj = new calc();
  }
}

i'm making a calculator using java swing, the output of this code is:

calc.java:48 error: incompatible types: double cannot be converted to String 
     jt3.setText(result);

i think this is not a big error, well help me to get rid of this, i just want to sum didn't add more functions like multiply or minus or etc, i just want to run as small code first then i'll add more functions to it well help will be appreciated thanks.

AbHi TheNua
  • 21
  • 1
  • 3
  • 1
    Can you explain which part of the error is not clear to you? – takendarkk Dec 11 '17 at 15:45
  • 1
    You're trying to set the text field to a double. Text fields take in a string value. Convert result to a string first then assign it. – benjiiiii Dec 11 '17 at 15:46
  • Java has a class `Double` with a static method `toString` which needs a `double` as parametre: https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#toString(double) – AntonH Dec 11 '17 at 15:46
  • 1
    Side note:`ae.getSource()=="+"` will not work, use "equals" for strings –  Dec 11 '17 at 15:48
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Dec 11 '17 at 15:51

4 Answers4

1

Easy way is:

//jt3.setText(result);
jt3.setText("" + result); 

This will force the compiler to create a String of the two values.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • thanks it is running but it is not adding two numbers in 3rd textfield, what's problem in this code? – AbHi TheNua Dec 12 '17 at 06:04
  • @AbHiTheNua, so the answer to the original question has been given. Glad it helped. Don't forget to "accept" the answer so people know the problem has been solved. – camickr Dec 12 '17 at 15:37
  • `it is not adding two numbers` - is the code being executed. Did you add debug code to the if statement. Did you read the comments about not using "==" for string comparison. If you still have problems, after following all the advice given then post a proper [mcve] in another question. And don't forget to name your class properly. Class names should start with an upper case character. – camickr Dec 12 '17 at 15:39
1

Use jt3.setText(String.valueOf(result));.
.setText() only accept String type.
You can see it in Class TextField.

KL_
  • 1,503
  • 1
  • 8
  • 13
1

Class text can accept only string values.
where the result you provided as an argument is Double

You can use this to convert it as a string

string converted = Double.toString(result);
Rahul
  • 230
  • 1
  • 3
  • 15
  • If you find the answer you wanted, Press the tick mark on the side, or if you have further questions, comment. – Rahul Dec 11 '17 at 15:57
0

This error is because JTextField is expecting a String to set the text to it, not a double, so, you need to either:

jt3.setText(String.valueOf(result));

Or

jt3.setText("" + result);

The first one will convert result to a String value, while the second one will concatenate an empty String to result, and return a String as well.

However one last suggestion I want you to take note of is, don't use single letter variables, make them more descriptive, for example:

JFrame f; //This should be JFrame frame;

The same for the JPanel and the rest of your variables, since in larger programs it could be hard to remember that f means a JFrame and not the conversion to Fahrenheit from Celsius or a formula like F = m * a, it may be confusing and really hard to debug / understand later on.

And also as has been said in the comments above, use .equals to compare String in Java, see How do I compare strings in Java? for more on this

Frakcool
  • 10,915
  • 9
  • 50
  • 89