gpt4 book ai didi

Java图形用户界面

转载 作者:行者123 更新时间:2023-12-01 23:11:53 24 4
gpt4 key购买 nike

我想通过从两个输入字段获取数据来在java中添加两个字符串。结果显示在第三个字段中。我正在使用此代码,但 netbeans 显示错误:

float num1,num2,result;
num1=Float.parseFloat(jTextField1.getText());
num2=Float.parseFloat(jTextField2.getText());
result=num1+num2;
jTextField3.setText(String.valueOf(result));

最佳答案

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.

如果您输入的文本不是正确的数字,您将收到 NumberFormatException。将 parseFloat() 语句放在 try-catch 中> 阻止。 “Ahsan”不是一个数字,因此无法解析。

对您的代码进行一点修改:

try{
num1=Float.parseFloat(jTextField1.getText());
num2=Float.parseFloat(jTextField2.getText());
result=num1+num2;
jTextField3.setText(String.valueOf(result));
}catch(NumberFormatException e){
e.printStackTrace();
}

关于Java图形用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21808264/

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