I'm fairly new to Java, I'm making a calculator and I cant seem to get Math.sqrt
to work on any number that isn't just a number (i.e. Math.sqrt(6 + 3
)). Please could someone tell me what I'm doing wrong.
The equals button:
//Equals button
JButton equals = new JButton();
equals.setText("=");
panel.add(equals);
frame.add(panel);
equals.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent eq) {
if(calculation.getText().contains("Math.sqrt")){
calculation.setText(calculation.getText().replace("Math.sqrt(", ""));
calculation.setText(Double.toString(Math.sqrt(Double.valueOf(calculation.getText()).longValue())));
}
else{
double answer = (Equals.eval(calculation.getText()));
Double answer1 = Double.valueOf(answer);
calculation.setText(Double.toString(answer));
}
}
});;
The Square root button:
//root
JButton root = new JButton();
root.setText("Sqrt");
panel.add(root);
frame.add(panel);
root.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
calculation.setText(calculation.getText() + "Math.sqrt(");
}
});;
Thanks.