尝试将 JTextField 转换为 Integer 时出现 NumberFormatException
NumberFormatException when trying to convert JTextField to Integer
我已经尝试了很多方法,但似乎仍然无法修复此错误,所以我要问自己的问题。
我一直认为错误出在我尝试将 JTextfield 转换为 int 的 ActionEvent 上,但它可能是其他原因?谢谢:)
我在另一个 class 中有一个对象,我需要将数据发送到该对象以构建它。这是对象
/**
* 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:"
您要求从 enterYourBalance
解析,但您的 JTextField
是 textBoxToEnterBalance
。
在您的代码中将相应的行替换为:
int balance = Integer.parseInt(textBoxToEnterBalance.getText().trim());
int balance = Integer.parseInt(enterYourBalance.getText().trim());
enterYourBalance 是 JLabel 对象
这样编辑
int balance = Integer.parseInt(textBoxToEnterBalance .getText().trim());
尝试从 JTextField 而不是从 JLabel 获取值
JLabel enterYourBalance = new JLabel("Current Balance:");
JTextField textBoxToEnterBalance = new JTextField(0);
...
int balance = Integer.parseInt(enterYourBalance.getText().trim());`
enterYourBalance.getText().trim()
将 return "Current Balance:"
解析为 int
失败。改成
textBoxToEnterBalance.getText().trim()
从文本字段中获取文本。
我已经尝试了很多方法,但似乎仍然无法修复此错误,所以我要问自己的问题。
我一直认为错误出在我尝试将 JTextfield 转换为 int 的 ActionEvent 上,但它可能是其他原因?谢谢:)
我在另一个 class 中有一个对象,我需要将数据发送到该对象以构建它。这是对象
/**
* 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:"
您要求从 enterYourBalance
解析,但您的 JTextField
是 textBoxToEnterBalance
。
在您的代码中将相应的行替换为:
int balance = Integer.parseInt(textBoxToEnterBalance.getText().trim());
int balance = Integer.parseInt(enterYourBalance.getText().trim());
enterYourBalance 是 JLabel 对象
这样编辑
int balance = Integer.parseInt(textBoxToEnterBalance .getText().trim());
尝试从 JTextField 而不是从 JLabel 获取值
JLabel enterYourBalance = new JLabel("Current Balance:");
JTextField textBoxToEnterBalance = new JTextField(0);
...
int balance = Integer.parseInt(enterYourBalance.getText().trim());`
enterYourBalance.getText().trim()
将 return "Current Balance:"
解析为 int
失败。改成
textBoxToEnterBalance.getText().trim()
从文本字段中获取文本。