Ceci est le code et quand je saisis une lettre il n'afficherait pas la ligne .matches à la place j'ai eu une erreur mais si je retire le parseFloat rec1 et enlève le else if (rec1 < total) puis la ligne .matches affichera s'il vous plaît aidez-moi comment faire cela merci d'avancejava.lang.NumberFormatException Pour la valeur de la chaîne d'entrée est
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");
}
// erreur
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)
Vous devez vérifier si elle correspond avant d'essayer l'analyse syntaxique. – shmosel
Et vous devez vérifier s'il est vide avant de vérifier s'il correspond. – shmosel
Vérifiez cette condition avant d'analyser la valeur 'if (! Receive.matches (" [0-9] + "))'. –