Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输入字符串值的java.lang.NumberFormatException为字母_Java_Jframe_Jtextfield - Fatal编程技术网

输入字符串值的java.lang.NumberFormatException为字母

输入字符串值的java.lang.NumberFormatException为字母,java,jframe,jtextfield,Java,Jframe,Jtextfield,这是代码,当我输入一个字母时,它不会显示该行。相反,我得到了一个错误,但是如果我删除parseFloat rec1并删除else if(rec1

这是代码,当我输入一个字母时,它不会显示该行。相反,我得到了一个错误,但是如果我删除parseFloat rec1并删除else if(rec1 CashType c=新的CashType(); c、 setVisible(真)


从错误消息中,看起来您正在向jTextField1输入“asdasd”。您正试图解析为浮动的此值
Float.parseFloat(string)
如果字符串不是数字,将抛出NumberFormatException。在
parseFloat()
方法中,字符串参数将转换为基本浮点值

您可以检查输入的值是否为数字,然后将其解析为float

float rec1 = 0;

   if(isNumeric(receive)){
       rec1 = Float.parseFloat(receive);
       if(rec1 < total){

         JOptionPane.showMessageDialog(null,"Insufficient Amount");

       }
   }else {
          JOptionPane.showMessageDialog(null,"Enter a Valid Amount");
            c.jTextField1.setText("");
         }

在尝试分析之前,您需要检查它是否匹配。在检查它是否匹配之前,您需要检查它是否为空。在分析值
if(!receive.matches([0-9]+”)之前,请检查此条件
。嘿,谢谢你。它帮助我解决了我的问题,但运行后出现了一个小问题。如果我输入一个字母,它将显示输入有效金额,然后按“确定”,将弹出“金额不足”的窗口。如何解决此问题?对于这些问题,我很抱歉,但我仍在学习java。再次感谢您提前检查更改的代码。这是因为if(rec1Exception 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)
float rec1 = 0;

   if(isNumeric(receive)){
       rec1 = Float.parseFloat(receive);
       if(rec1 < total){

         JOptionPane.showMessageDialog(null,"Insufficient Amount");

       }
   }else {
          JOptionPane.showMessageDialog(null,"Enter a Valid Amount");
            c.jTextField1.setText("");
         }
 public static boolean isNumeric(String s) {  
    return s.matches("[-+]?\\d*\\.?\\d+");  
}