jframe java。输入字符串值的lang.NumberFormatException为字母
这是代码,当我输入一个字母时,它不会显示该行。相反,我得到了一个错误,但是如果我删除了parseFloat rec1并删除了else if(rec1<;total),那么。火柴线将显示,请帮助我如何做,谢谢提前
CashType c=新的CashType(); c、 setVisible(真)
c.jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
String receive = c.jTextField1.getText();
float total = total("sellno"+SellNoCount.getText());
float rec1 = Float.parseFloat(receive); //this is line 1525
if(!receive.matches("[0-9]+")){
JOptionPane.showMessageDialog(null,"Enter a Valid Amount");
c.jTextField1.setText("");
}
else if(receive.equalsIgnoreCase("")){
JOptionPane.showMessageDialog(null,"Enter Amount");
}
else if(rec1 < total){
JOptionPane.showMessageDialog(null,"Insufficient Amount");
}
//错误
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "asdasd"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at projectfinal.SellPage$32.actionPerformed(SellPage.java:1525)
# 1 楼答案
从错误消息来看,您似乎正在向jTextField1输入“asdasd”。您试图解析为浮动的该值^如果字符串不是数字,{}将抛出NumberFormatException。在
parseFloat()
方法中,字符串参数将转换为基本浮点值您可以检查输入的值是否为数字,然后将其解析为float
方法是