gpt4 book ai didi

java - 尝试将 JTextField 转换为 Integer 时出现 NumberFormatException

转载 作者:行者123 更新时间:2023-12-02 04:56:06 29 4
gpt4 key购买 nike

我已经尝试了很多方法,但无论如何我似乎仍然无法修复这个错误,所以我要问我自己的问题。

我一直相信错误出现在 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:"

最佳答案

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() 从文本字段获取文本。

关于java - 尝试将 JTextField 转换为 Integer 时出现 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28760619/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com