尝试将JTextField转换为Integer时的NumberFormatException

时间:2015-02-27 08:35:35

标签: java user-interface object constructor jtextfield

我尝试过很多事情,但无论如何我都无法解决这个错误。

我一直认为错误发生在ActionEvent,我尝试将JTextfield转换为int,但它可能是其他的东西?谢谢:))

我在另一个类中有一个对象,我需要将数据发送到它来构造它。这是对象

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

    numOfAccounts++;
}

现在剩下的代码,

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); 
}


}

我收到的错误消息是

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

3 个答案:

答案 0 :(得分:2)

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

enterYourBalance.getText().trim()将返回"Current Balance:"并将其解析为int失败。改成 textBoxToEnterBalance.getText().trim()从文本字段中获取文本。

答案 1 :(得分:0)

您要求从enterYourBalance解析,但您的JTextFieldtextBoxToEnterBalance。 在您的代码中,用以下代码替换相应的行:

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

答案 2 :(得分:0)

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

enterYourBalance是JLabel对象

像这样编辑

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

尝试从JTextField获取值而不是从JLabel获取