Nina2507
Messages postés32Date d'inscriptionmercredi 1 novembre 2006StatutMembreDernière intervention24 avril 2008
-
15 déc. 2006 à 21:27
sheorogath
Messages postés2448Date d'inscriptionsamedi 21 février 2004StatutModérateurDernière intervention29 janvier 2010
-
15 déc. 2006 à 22:09
bsr;
je travaille sur un projet qui consiste à implementer la méthode du simplex sous java.
à une etape je dois récupérer des données de type float d'une zine de texte.
voici le code ke jé fé:
import javax.swing.*;
public class NumText extends JTextField {
public NumText(NumDocument doc){
super(doc,"",4);
}
public float getFloat(){
if(this.getText() == ""){
return 0;
}
return Float.parseFloat(this.getText());
}
la méthde getFloat doit marcher ; mé elle me génére une exception:
java.lang.NumberFormatException: empty String
at java.lang.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Float.parseFloat(Unknown Source)
at simplex.NumText.getFloat(NumText.java:25)
please, jattend votre aide pr pouvoir avancé ds mon projet.
merci
cs_laurent1024
Messages postés987Date d'inscriptionmardi 31 mai 2005StatutMembreDernière intervention30 août 201225 15 déc. 2006 à 22:04
ATTENTION
this.getText() == "" ca teste pas si le contenu est égal a ""
il faut faire this.getText().equals("") ou this.getText().compareTo("") = 0
au final tu as ca :
if(this.getText() != null && this.getText().equals(""))
return 0;
else
try {
float f = Float.ParseFloat(this.getText());
return f;
}
catch(Exception e) {
// pas a bon format
e.printStackTrace();
return 0;
}