rpm shuvo rpm shuvo - 11 days ago 5
Java Question

The operator + is undefined for the argument type(s) java.lang.Double, java.lang.Double

i want to create a calculator .my code run in "cmd" successfully .but it didn't run in eclipse.it shows error in line 79."Double result =d1+d2;" here.
error show that:
The operator + is undefined for the argument type(s) java.lang.Double, java.lang.Double
please help.


part of a class


try{
String s1 = txtFirst.getText(); String s2 = txtSecond.getText();
Double d1= Double.valueOf(s1);
Double d2= Double.valueOf(s2);
Double result = d1+d2 ;
String s = String.valueOf(result);
txtResult.setText(s);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Invalid Input", "Input Error", JOptionPane.INFORMATION_MESSAGE);
}

Answer

You may be using old version of java which doesn't use AutoBoxing". Hence either upgarde java or use "double" instead of "Double" like this.

try{
    String s1 = txtFirst.getText(); String s2 = txtSecond.getText();
    double d1= Double.parseDouble(s1);
    double d2= Double.parseDouble(s2);
    double result = d1+d2 ; 
    String s = String.valueOf(result);
    txtResult.setText(s);
}catch(Exception ex){
    JOptionPane.showMessageDialog(null, "Invalid Input", "Input Error", JOptionPane.INFORMATION_MESSAGE);
}
Comments