Reputation:
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);
}
Upvotes: 1
Views: 1487
Reputation: 430
Which java version are you using ? because Autoboxing and Unboxing came in Java 5.0 If you are using old java version then you can't typecast directly.In that case you need to use below code
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);
}
Upvotes: 0
Reputation: 9188
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);
}
Upvotes: 1