0

I was trying to make a program which can do the quadratic formula. I've also done this as a console program and it worked but now I want do do it with a GUI. Now my problem is, that it just shows NaN. I've copied the formula from the working console program, so that can't be the problem. I think that it has to do something with the parsing. Here you can see the code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Mitternachtsformel implements ActionListener {
JFrame frame;
JPanel panel;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JTextField text;
JTextField text1;
JTextField text2;
JLabel label;
JLabel label1;
JLabel label2;
JTextArea textA;
double aZahl;
double bZahl;
double cZahl;
double x1;
double x2;


public static void main(String[] args) {
    Mitternachtsformel MF = new Mitternachtsformel();
    MF.launchScreen();

}
public void launchScreen() {
    //frame erstellen
    frame = new JFrame("Mitternachtsformel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    //font erstellen
    Font font = new Font("impact", 50, 50);
    //panel erstellen
    panel = new JPanel();
    panel.setSize(500, 500);
    panel.setBackground(Color.white);
    //panel1 erstellen
    panel1 = new JPanel();
    panel1.setSize(500, 500);
    panel1.setBackground(Color.white);
    panel1.setVisible(false);
    //panel1 erstellen
    panel2 = new JPanel();
    panel2.setSize(500,  500);
    panel2.setBackground(Color.white);
    panel2.setVisible(false);
    //panel3 erstellen
    panel3 = new JPanel();
    panel3.setSize(500, 500);
    panel3.setBackground(Color.white);
    panel3.setVisible(false);
    //label erstellen
    label = new JLabel("Geben sie einen Wert für a an: ");
    label1 = new JLabel("Geben sie einen Wert für b an: ");
    label2 = new JLabel("Geben sie einen Wert für c an: ");
    //textField erstellen
    text = new JTextField(10);
    text.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent e)
            {

                panel.setVisible(false);
                panel1.setVisible(true);
            }
            });
    //textField1 erstellen
    text1 = new JTextField(10);
    text1.requestFocus();
    text1.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {

            panel1.setVisible(false);
            panel2.setVisible(true);
        }
        });
    //textField2 ertsellen
    text2 = new JTextField(10);
    text2.requestFocus();
    text2.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent e)
            {

                panel2.setVisible(false);
                panel3.setVisible(true);
            }
            });
    try {
        aZahl = Double.parseDouble(text.getText());
        bZahl = Double.parseDouble(text1.getText());
        cZahl = Double.parseDouble(text2.getText());
        }
    catch(Exception ex) {

    }
    try {
     x1 = (-bZahl + (Math.sqrt((bZahl*bZahl) - 4 * aZahl *cZahl))) / (2*aZahl);
     x2 = (-bZahl - (Math.sqrt((bZahl*bZahl) - 4 * aZahl *cZahl))) / (2*aZahl);
     }
    catch(Exception ex) {

    }



    //textArea erstellen
    textA = new JTextArea(x1 + ", " + x2);
    //add- things
    panel.add(label);
    panel.add(text);
    panel1.add(label1);
    panel1.add(text1);
    panel2.add(label2);
    panel2.add(text2);
    panel3.add(textA);
    frame.add(panel3);
    frame.add(panel2);
    frame.add(panel1);
    frame.add(panel);
    frame.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {     
}
}

I would be so glad if you could help me, because I was trying to solve the problem for about 5h and it is just frustrading me.

Thank you

sin-an
  • 33
  • 1
  • 1
  • 5
  • What are your `aZahl`, `bZahl`, and `cZahl` values? You may be trying to take the square root of a negative number. – rgettman Aug 09 '18 at 20:02
  • I think if you fix your formatting you might see a problem. – 001 Aug 09 '18 at 20:06
  • This is a bit of an odd gui. Why not just create 3 text boxes for input, one button to solve (hint: the code to solve the equation should be in this button's event handler), and then another text box to display result? – 001 Aug 09 '18 at 20:16
  • Probably when you are hiding the text box and then access the value it is null. – Mark Stewart Aug 09 '18 at 20:25
  • Remove all of your try/catch statements. An exception means an operation did not succeed, and you should not continue as if it did succeed. If Double.parseDouble fails, you don’t have a valid number, so you certainly don’t want the code to proceed. – VGR Aug 10 '18 at 01:20

1 Answers1

0

As far as I can tell with the formatting, the code where you calculate the values is not in any event handler. Here's a version of your program that uses a single panel and a button to do the calculation. (Just keep in mind - not all input will produce a result. You can't take the square root of a negative number)

public void launchScreen() {
    JFrame frame = new JFrame("Mitternachtsformel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    //panel erstellen
    JPanel panel = new JPanel();
    panel.setSize(500, 500);
    panel.setBackground(Color.white);

    // Using a grid layout instead of multiple panels
    GridLayout layout = new GridLayout(0,2);
    panel.setLayout(layout);

    JTextField text1 = new JTextField(10);
    JTextField text2 = new JTextField(10);
    JTextField text3 = new JTextField(10);
    JTextField result = new JTextField(10);
    JButton solveButton = new JButton("Solve");
    // Do the calculation when the "Solve" button is pressed
    solveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            try {
                double aZahl = Double.parseDouble(text1.getText());
                double bZahl = Double.parseDouble(text2.getText());
                double cZahl = Double.parseDouble(text3.getText());
                double x1 = (-bZahl + (Math.sqrt((bZahl*bZahl) - 4 * aZahl *cZahl))) / (2*aZahl);
                double x2 = (-bZahl - (Math.sqrt((bZahl*bZahl) - 4 * aZahl *cZahl))) / (2*aZahl);
                result.setText(x1 + ", " + x2);
            }
            catch(Exception ex) {
                result.setText("ERROR: " + ex.getMessage());
            }
        }
    });

    panel.add(new JLabel("Geben sie einen Wert für a an: "));
    panel.add(text1);
    panel.add(new JLabel("Geben sie einen Wert für b an: "));
    panel.add(text2);
    panel.add(new JLabel("Geben sie einen Wert für c an: "));
    panel.add(text3);
    panel.add(solveButton);
    panel.add(result);
    frame.add(panel);
    frame.setVisible(true);
}

enter image description here

001
  • 13,291
  • 5
  • 35
  • 66