这是代码,当我输入一个字母它不会显示行.matches相反,我得到一个错误,但如果我删除了parseFloat rec1并删除其他if( rec1 < total)那么.matches行会显示请帮助我如何提前谢谢java.lang.NumberFormatException对于输入字符串值是字母
CashType c = new CashType(); c.setVisible(true);
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)
您需要检查,如果它试图解析之前匹配。 – shmosel
你需要检查它是否为空,然后检查它是否匹配。 – shmosel
在解析值'if(!receive.matches(“[0-9] +”))''之前检查此条件。 –