java.lang.NumberFormatException 对于输入字符串值是字母
java.lang.NumberFormatException For Input String value is letters
这是代码,当我输入一个字母时,它不会显示 .matches 行,而是出现错误,但是如果我删除 parseFloat rec1 并删除 else if (rec1 < total) then the .matches 行会显示请帮助我如何做到这一点提前谢谢你
CashType c = new 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.actionPerformed(SellPage.java:1525)
从错误消息来看,您似乎正在向 jTextField1 输入 "asdasd"。您尝试解析为浮点数的这个值。如果字符串不是数字,Float.parseFloat(string)
将抛出 NumberFormatException。在parseFloat()
方法中,字符串参数将被转换为原始浮点值。
您可以检查输入的值是否为数字,然后将其解析为浮点数。
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+");
}
这是代码,当我输入一个字母时,它不会显示 .matches 行,而是出现错误,但是如果我删除 parseFloat rec1 并删除 else if (rec1 < total) then the .matches 行会显示请帮助我如何做到这一点提前谢谢你
CashType c = new 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.actionPerformed(SellPage.java:1525)
从错误消息来看,您似乎正在向 jTextField1 输入 "asdasd"。您尝试解析为浮点数的这个值。如果字符串不是数字,Float.parseFloat(string)
将抛出 NumberFormatException。在parseFloat()
方法中,字符串参数将被转换为原始浮点值。
您可以检查输入的值是否为数字,然后将其解析为浮点数。
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+");
}