gpt4 book ai didi

java - 将字符串转换为整数时出现异常

转载 作者:行者123 更新时间:2023-12-01 18:30:27 25 4
gpt4 key购买 nike

我是 Java 初学者。我使用 Netbeans。以下是我的代码片段,用于制作一个简单的计算器来添加两个数字。不包括按钮的自生成代码。

问题

当我尝试在函数 plus_buttonActionPerformedequal_buttonActionPerformed 中将字符串 str 转换为整数 num1 时给出一个异常,指出:

 exception in thread awt-eventqueue-0 java.lang.numberformatexception for input string  

我通过将字符串打印在转换语句上方来确保该字符串不为空。代码有点长。赦免。我在这里做错了什么。

 public class calc extends javax.swing.JFrame {

/**
* Creates new form calc
*/
public String str = " ";
public String action = " ";
public int num1;
public int num2;
public int res;
public calc() {
initComponents();
}
private void button3ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("3");

result.setText(str);

}

private void button6ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("6");
result.setText(str);

}

private void button9ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("9");
result.setText(str);
}

private void resultActionPerformed(java.awt.event.ActionEvent evt) {

result.setText(str);//
}

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("1");
result.setText(str);//
}

private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("2");
result.setText(str);
}

private void button4ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("4"); //
result.setText(str);
}

private void button5ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("5"); //
result.setText(str);
}

private void button7ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("7");
result.setText(str);//
}

private void button8ActionPerformed(java.awt.event.ActionEvent evt) {
str=str.concat("8");
result.setText(str);//
}

private void plus_buttonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(str);
num1=Integer.parseInt(str);
System.out.println(num1);
str=" ";

}

private void equal_buttonActionPerformed(java.awt.event.ActionEvent evt) {
num2=Integer.parseInt(str);
res=num1+num2;
str=""+res;
result.setText(str);
}


public static void main(String args[]) {


try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new calc().setVisible(true);
}
});
}

最佳答案

作为docs state ,在以下情况下会发生 NumberFormatException:

Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

您尝试解析的字符串格式不是有效的整数。事实上,你的字符串的值是一个空格。

首先,您可以使用trim()删除字符边缘的空白。即使将其初始化为空字符串:String str = ""; 也会发生 NumberFormatException,因为它不是有效的整数。

因此,为其分配一个非空整数值,它将正确解析。

关于java - 将字符串转换为整数时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24450191/

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