Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculator - setText(String) doesn't work with Doubles

So I am trying to make a calculator and I am using JFrame code and trying to use the built in Math to find the square root of a number. The code below is where I am having issues. "display.setText(Math.sqrt(Double.parseDouble(display.getText())));"

gives me the error of "The method setText(String) in the type JTextComponent is not applicable for the arguments (double)"

    sqrt = new JButton("SQRT");
    sqrt.setBounds(298, 141, 65, 65);
    sqrt.setBackground(Color.BLACK);
    sqrt.setForeground(Color.BLACK);
    sqrt.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setTempFirst(Double.parseDouble(display.getText()));
            display.setText(Math.sqrt(Double.parseDouble(display.getText())));
            operation[5] = true;

        }
    });
    add(sqrt);
like image 629
Max E Avatar asked Apr 17 '26 23:04

Max E


1 Answers

Since Math.sqrt returns a double you can not do:

display.setText(Math.sqrt(Double.parseDouble(display.getText())));

instead you can use the value returned by that method and use the String.valueOf() like:

display.setText(String.valueOf(Math.sqrt(....
like image 156
ΦXocę 웃 Пepeúpa ツ Avatar answered Apr 20 '26 11:04

ΦXocę 웃 Пepeúpa ツ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!