Ashley Palmer
Ashley Palmer

Reputation: 3

NumberFormatException when trying to convert JTextField to Integer

I've tried many things and I still can't seem to fix this error no matter what so I'm going to ask my own question.

I keep believing that the error is at the ActionEvent where I try to convert the JTextfield into an int but it might be something else? Thanks :)

I have an object in another class that I need to send the data to in order to construct it. This is the object

/**
 * Constructor
 */
public Account(int startAmount, int balance, int credit, String Name, String    Address)  {       
    openingBalance = startAmount;
    currentBalance = balance;
    creditLimit = credit;
    accountName = Name;
    accountAddress = Address;

    numOfAccounts++;
}

And now the rest of my code,

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

public class AccountGUI extends JFrame{


private static final long serialVersionUID = 1L;

String Name;
String Address;
int balance;
int credit;
GridBagConstraints gbc = new GridBagConstraints();

public AccountGUI(){

    setLayout(new GridBagLayout());

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.ipadx = 20;
    gbc.ipady = 20;

    JLabel enterYourName = new JLabel("Name:");
    JTextField textBoxToEnterName = new JTextField(20);
    JPanel firstPanel = new JPanel();
    addHelper(enterYourName,0,0);
    addHelper(textBoxToEnterName,1,0);

    JLabel enterYourAddress = new JLabel("Address:");
    JTextField textBoxToEnterAddress = new JTextField(20);
    addHelper(enterYourAddress,0,1);
    addHelper(textBoxToEnterAddress,1,1);

    JLabel enterYourBalance = new JLabel("Current Balance:");
    JTextField textBoxToEnterBalance = new JTextField(0);
    addHelper(enterYourBalance, 0,2);
    addHelper(textBoxToEnterBalance, 1,2);

    JLabel enterYourCreditLimit = new JLabel("Credit Limit:");
    JTextField textBoxToEnterCreditLimit = new JTextField(20);
    addHelper(enterYourCreditLimit, 0, 3);
    addHelper(textBoxToEnterCreditLimit, 1,3);

    JButton submit = new JButton("Submit");
    submit.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            Name = enterYourName.getText();
            Address = enterYourAddress.getText();
            try {
                int balance = Integer.parseInt(enterYourBalance.getText().trim());
            }
            catch(NumberFormatException ex)
            {
                System.out.println("Exception : "+ex);
            }
            try {
                int credit = Integer.parseInt(enterYourCreditLimit.getText().trim());
            }
            catch(NumberFormatException ex)
            {
                System.out.println("Exception : "+ex);
            }
            Account record = new Account(0, balance, credit, Name, Address);
        }
    });
    addHelper(submit,0,4);

    setTitle("AccountGUI");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);


}

private void addHelper(JComponent item, int x, int y){
    gbc.gridx= x;
    gbc.gridy= y;
    add(item,gbc);
}

public static void main(String[] args) {
    AccountGUI promptForName = new AccountGUI();
    promptForName.setVisible(true); 
}


}

The error message I get is

Exception : java.lang.NumberFormatException: For input string: "Current Balance:" 
Exception : java.lang.NumberFormatException: For input string: "Credit Limit:"

Upvotes: 0

Views: 457

Answers (3)

Kishore
Kishore

Reputation: 839

JLabel enterYourBalance = new JLabel("Current Balance:");
JTextField textBoxToEnterBalance = new JTextField(0);
...
int balance = Integer.parseInt(enterYourBalance.getText().trim());`

enterYourBalance.getText().trim() will return "Current Balance:" and parsing it to int fails. Change to textBoxToEnterBalance.getText().trim() to get the text from the text field.

Upvotes: 2

Anptk
Anptk

Reputation: 1123

int balance = Integer.parseInt(enterYourBalance.getText().trim());

enterYourBalance is JLabel object

Edit like this

    int balance = Integer.parseInt(textBoxToEnterBalance .getText().trim());

Try to get value from JTextField Not from JLabel

Upvotes: 0

osechet
osechet

Reputation: 466

You ask to parse from enterYourBalance but your JTextField is textBoxToEnterBalance. In your code replace the corresponding line by:

int balance = Integer.parseInt(textBoxToEnterBalance.getText().trim());

Upvotes: 0

Related Questions