ntoonio
ntoonio

Reputation: 3143

Percents calculating part does not work in java

[Now working!] I have this code that should calculate percents (With some GUI). But the calculating part is not working... Maybe some of you know the answer? I think the problem is when I concert it from a string do a double. I'm from Sweden so I have a very crappy english. And all variable names were in swedish so I just translated them with googles translate.

package Java;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class java {


public static void main (String []args){

    int frameSizeX = 500;
    int frameSizeY = 135;

    JPanel p = new JPanel();
    p.setPreferredSize( new Dimension(frameSizeX,frameSizeY) );

    JFrame f = new JFrame ("Percentage");
    f.setSize(frameSizeX,frameSizeY);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.getContentPane().add(p);

    JLabel cardiNum = new JLabel ("Cardinal number");

    JTextField cardiNumField = new JTextField();
    cardiNumField.setPreferredSize( new Dimension( 500, 24 ) ); 

    final String talValue = (cardiNumField.getText());

    JLabel perc = new JLabel("Percentage");

    JTextField percField = new JTextField(); 
    percField.setPreferredSize( new Dimension( 500, 24 ) );

    final String proValue = (percField.getText());

    double D = 0;

    final JLabel answer = new JLabel("The answer is = " + D);

    JButton butt = new JButton("Calculate");
    butt.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {

            double A = Double.parseDouble(talValue);
            double B = Double.parseDouble(proValue);
            double C = A/100;
            double D = C*B;

              answer.setText("The answer is" + D);
          }
        });



    p.add(cardiNum, BorderLayout.AFTER_LINE_ENDS);
    p.add(cardiNumField, BorderLayout.AFTER_LINE_ENDS);
    p.add(perc, BorderLayout.AFTER_LINE_ENDS);
    p.add(percField, BorderLayout.AFTER_LINE_ENDS);
    p.add(butt);
    p.add(answer);

    f.pack();
    f.setVisible(true);

}
}

Upvotes: 0

Views: 131

Answers (2)

Steve
Steve

Reputation: 9480

As Martin Dinov mentions, the problem is that you were reading the contents of the text boxes before the user was able to write in them and click the button. That leads to a NumberFormatException, because you're trying to convert the empty String "" to a double. Therefore, you need to put that code inside the button click event handler. An extra change to fix this is that you need to make your text fields final. i.e.

final JTextField cardiNumField = new JTextField();
final JTextField percField = new JTextField();

That's because the button click handler is an anonymous inner class, so it can only read from final variables.

The following code fixes the problem I think you have been seeing.

public static void main(String[] args) {

    int frameSizeX = 500;
    int frameSizeY = 135;

    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(frameSizeX, frameSizeY));

    JFrame f = new JFrame("Percentage");
    f.setSize(frameSizeX, frameSizeY);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.getContentPane().add(p);

    JLabel cardiNum = new JLabel("Cardinal number");

    final JTextField cardiNumField = new JTextField();
    cardiNumField.setPreferredSize(new Dimension(500, 24));

    JLabel perc = new JLabel("Percentage");

    final JTextField percField = new JTextField();
    percField.setPreferredSize(new Dimension(500, 24));

    double D = 0;

    final JLabel answer = new JLabel("The answer is = " + D);

    JButton butt = new JButton("Calculate");
    butt.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {

            double cardinal = Double.parseDouble(cardiNumField.getText());
            double percentage = Double.parseDouble(percField.getText());

            double C = cardinal / 100;
            double D = C * percentage;

            answer.setText("The answer is " + D);
        }
    });

    p.add(cardiNum, BorderLayout.AFTER_LINE_ENDS);
    p.add(cardiNumField, BorderLayout.AFTER_LINE_ENDS);
    p.add(perc, BorderLayout.AFTER_LINE_ENDS);
    p.add(percField, BorderLayout.AFTER_LINE_ENDS);
    p.add(butt);
    p.add(answer);

    f.pack();
    f.setVisible(true);

}

Good luck with learning Java. You're obviously very new to it! :)

Upvotes: 0

Martin Dinov
Martin Dinov

Reputation: 8825

You're getting the NullPointerException because you need to set those two strings only once the JButton has been clicked, inside the ActionListener.

Put these two lines:

final String talValue = (cardiNumField.getText());
final String proValue = (percField.getText());

inside the ActionListener for the JButton and make the cardiNumField and percField final and it should work. Alternatively, you can just remove those two variables and have:

double A = Double.parseDouble(cardiNumField.getText());
double B = Double.parseDouble(percField.getText());

Upvotes: 1

Related Questions