将 jtextfield 转换为 int 的 Numberformat 异常
Numberformat exception for converting jtextfield to int
我一直在查看其他帖子的答案,它们似乎指向我正在做的事情,但我仍然得到 NumberFormatException
。我正在尝试通过提取 jtextfield 值并将其转换为 int 来从 JFrame
构建一个简单的计算器,但我遇到了以下异常。
package com.calculator.app;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CalculatorApp extends JFrame {
private JPanel contentPane;
private JTextField textField = new JTextField();
private JTextField textField_1 = new JTextField();
private JTextField textField_2 = new JTextField();
int num1 = Integer.parseInt(textField.getText());
int num2 = Integer.parseInt(textField_1.getText());
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalculatorApp frame = new CalculatorApp();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CalculatorApp() {
// int num2 = Integer.parseInt(value2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblCalculatorApp = new JLabel("Calculator App");
lblCalculatorApp.setBounds(141, 37, 138, 23);
contentPane.add(lblCalculatorApp);
JButton btnNewButton = new JButton("Add");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnNewButton.setBounds(74, 112, 131, 31);
contentPane.add(btnNewButton);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBounds(169, 181, 82, 23);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(55, 73, 54, 23);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(151, 70, 54, 23);
contentPane.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(109, 162, 124, 55);
contentPane.add(textField_2);
// textField_2.setText(String.valueOf(output));
try{
int output = Integer.parseInt(textField.getText());
}
catch(NumberFormatException e){
System.out.println("Error");
}
}
}
异常
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.calculator.ap.CalculatorApp.<init>(CalculatorApp.java:23)
at com.calculator.ap.CalculatorApp.run(CalculatorApp.java:32)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这一行:
int num1 = Integer.parseInt(textField.getText());
等同于
int num1 = Integer.parseInt("");
因为您没有在这一行中使用特定值初始化 textField
:
private JTextField textField = new JTextField();
因此,您试图将空字符串解析为整数,但它抛出 NumberFormatException
,因为它不是整数。
如果你只是想解决这些问题,你可以在此处输入 3 个整数:
private JTextField textField = new JTextField("1");
private JTextField textField_1 = new JTextField("2");
private JTextField textField_2 = new JTextField("3");
或者(更好)您可以删除以下行,因为您似乎需要稍后解析它们(单击按钮时):
int num1 = Integer.parseInt(textField.getText());
int num2 = Integer.parseInt(textField_1.getText());
上面的解释可以帮助您避免该异常。
但是如果你想对这些数字求和,你应该填写这个方法的主体:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text1 = textField.getText();
String text2 = textField_1.getText();
textField_2.setText(getIntegerValue(text1) + getIntegerValue(text2) + "");
}
});
其中 getIntegerValue
可以是这样的:
private int getIntegerValue(String s) {
if ( "".equals(s) ) {
return 0;
}
return Integer.parseInt(s);
}
或更短:
private int getIntegerValue(String s) {
return "".equals(s) ? 0 : Integer.parseInt(s);
}
这个方法:
- returns
0
如果字符串为空(无值)
- returns 整数如果字符串表示整数
- 如果字符串不是整数或空字符串,则抛出
NumberFormatException
- 不需要捕捉
NumberFormatException
,因为它是一个RuntimeException
当然,如果即使字符串为空也想抛出异常,可以从最后一个方法中删除 if
/ 三元运算符。
我一直在查看其他帖子的答案,它们似乎指向我正在做的事情,但我仍然得到 NumberFormatException
。我正在尝试通过提取 jtextfield 值并将其转换为 int 来从 JFrame
构建一个简单的计算器,但我遇到了以下异常。
package com.calculator.app;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CalculatorApp extends JFrame {
private JPanel contentPane;
private JTextField textField = new JTextField();
private JTextField textField_1 = new JTextField();
private JTextField textField_2 = new JTextField();
int num1 = Integer.parseInt(textField.getText());
int num2 = Integer.parseInt(textField_1.getText());
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalculatorApp frame = new CalculatorApp();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CalculatorApp() {
// int num2 = Integer.parseInt(value2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblCalculatorApp = new JLabel("Calculator App");
lblCalculatorApp.setBounds(141, 37, 138, 23);
contentPane.add(lblCalculatorApp);
JButton btnNewButton = new JButton("Add");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnNewButton.setBounds(74, 112, 131, 31);
contentPane.add(btnNewButton);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBounds(169, 181, 82, 23);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(55, 73, 54, 23);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(151, 70, 54, 23);
contentPane.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(109, 162, 124, 55);
contentPane.add(textField_2);
// textField_2.setText(String.valueOf(output));
try{
int output = Integer.parseInt(textField.getText());
}
catch(NumberFormatException e){
System.out.println("Error");
}
}
}
异常
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.calculator.ap.CalculatorApp.<init>(CalculatorApp.java:23)
at com.calculator.ap.CalculatorApp.run(CalculatorApp.java:32)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这一行:
int num1 = Integer.parseInt(textField.getText());
等同于
int num1 = Integer.parseInt("");
因为您没有在这一行中使用特定值初始化 textField
:
private JTextField textField = new JTextField();
因此,您试图将空字符串解析为整数,但它抛出 NumberFormatException
,因为它不是整数。
如果你只是想解决这些问题,你可以在此处输入 3 个整数:
private JTextField textField = new JTextField("1");
private JTextField textField_1 = new JTextField("2");
private JTextField textField_2 = new JTextField("3");
或者(更好)您可以删除以下行,因为您似乎需要稍后解析它们(单击按钮时):
int num1 = Integer.parseInt(textField.getText());
int num2 = Integer.parseInt(textField_1.getText());
上面的解释可以帮助您避免该异常。
但是如果你想对这些数字求和,你应该填写这个方法的主体:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String text1 = textField.getText();
String text2 = textField_1.getText();
textField_2.setText(getIntegerValue(text1) + getIntegerValue(text2) + "");
}
});
其中 getIntegerValue
可以是这样的:
private int getIntegerValue(String s) {
if ( "".equals(s) ) {
return 0;
}
return Integer.parseInt(s);
}
或更短:
private int getIntegerValue(String s) {
return "".equals(s) ? 0 : Integer.parseInt(s);
}
这个方法:
- returns
0
如果字符串为空(无值) - returns 整数如果字符串表示整数
- 如果字符串不是整数或空字符串,则抛出
NumberFormatException
- 不需要捕捉
NumberFormatException
,因为它是一个RuntimeException
当然,如果即使字符串为空也想抛出异常,可以从最后一个方法中删除 if
/ 三元运算符。