-4

I created a loan calculator. but its not taking value for calculation. could you please help me to find what did go wrong please.

import java.awt.*;
import javax.swing.*;



public void initUIPanel() {
    jf = new JFrame();
    jf.setTitle("Mortgage Calculator");
    jf.setLocation(100, 200);
    jf.setSize(530, 740);
    jf.setVisible(true);
    jf.setResizable(false);


    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    JPanel pane = new JPanel(gbl);
    JPanel pane1 = new JPanel(gbl);
    JPanel pane2 = new JPanel(gbl);
    gbc.anchor = GridBagConstraints.WEST;



    gbc.weightx = 1.0;
    gbc.insets = new Insets(30, 3, 3, 3);
    loanAmount = new JTextField(12);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(loanAmount, gbc);
    pane.add(loanAmount);



    loanTerm = new JTextField(15);
    gbc.weightx = 1.0;
    gbc.insets = new Insets(20, 3, 3, 3);
    loanAmount = new JTextField(12);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(loanAmount, gbc);
    pane.add(loanAmount);




    gbc.insets = new Insets(30, 0, 3, 0);
    interestRate = new JTextField(4);
    gbc.gridx++;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(interestRate, gbc);
    pane2.add(interestRate);


    b1 = new JButton("Calculate");
    b1.setPreferredSize(new Dimension(5, 25));
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.insets = new Insets(20, 30, 0, 30);
    b1.addActionListener(this);
    gbl.setConstraints(b1, gbc);
    pane2.add(b1);




    jf.add(pane, BorderLayout.NORTH);
    jf.add(pane2, BorderLayout.SOUTH);
    jf.setVisible(true);
    jf.pack();


}

public void actionPerformed(ActionEvent e) {


    double amt, rate, pay;
    int time;
    String name;

    if (e.getSource() == b1) {
        amt = Double.parseDouble(loanAmount.getText());
        rate = Double.parseDouble(interestRate.getText());
        time = Integer.parseInt(loanTerm.getText());


        System.out.println(amt);
        System.out.println(rate);
        System.out.println(time);


    } else {
        jf.dispose();
    }
}




public static void main(String[] args) {
    // TODO Auto-generated method stub
    MortgageCalculator mc = new MortgageCalculator();
    mc.initUIPanel();



}

}

I have different class for

I have different class for calculation, but it is atleast not taking the value even in the same class. When i enter any value, it displays exception errors. I have edited the codes, shortening them. I am getting the error of

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" even when i put value in the textAreas.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Shasi
  • 274
  • 6
  • 21

1 Answers1

2

You never add the JTextField loanTerm to the JPanel pane so it remains with its default empty String value. When the ActionListener occurs, the empty String causes a NumberFormatException.

Add the JTextField to the container:

pane.add(loanTerm);
Reimeus
  • 158,255
  • 15
  • 216
  • 276