Int String Convert

cs_Taz1984 Messages postés 47 Date d'inscription lundi 20 juillet 2009 Statut Membre Dernière intervention 13 mars 2013 - 16 déc. 2009 à 17:29
 Utilisateur anonyme - 17 déc. 2009 à 01:10
Bonjour,

1) j'ai cette fonction :
public static int convertStringToInt(String string) {
int i = 0;

if (string != null && !"".equals(string)) {

i = Integer.parseInt(string);

}

return i;
}

supposons qu'en entré j'ai le string: "1XXXXX" losque celui ci passe cette fonction et bien ca plante.
j'aimerai faire un controle avant sur ce string pour bien vérifier qu'il contient bien que des chiffres.

Je ne vois pas trop comment faire , merci d'avance pour votre aide .

2 réponses

cs_jojolemariole Messages postés 519 Date d'inscription mercredi 21 mars 2007 Statut Membre Dernière intervention 19 décembre 2016 25
16 déc. 2009 à 18:02
Salut,

Ca ne plante pas, ça lève une exception (NumberFormatException). C'est très différent, rien ne t'empêche de rattraper l'exception levée.

SOLUTION 1

public static int convertStringToInt(String string) {
int i = 0;

if (string != null && !"".equals(string)) {

try {
i = Integer.parseInt(string);
} catch (NumberFomatException e) {
i = 0; // par exemple
}

}

return i;
}


SOLUTION 2

Tu peux effectivement contrôler que la chaîne de caractères passée en paramètre ne contient que des chiffres, mais ça ne te garantit pas que le parseInt ne lève pas une exception, car l'entier maximal est 2147483647 (au-dessus ça lèvera une exception).

Si tu t'en fous de la taille maximum de l'entier tu peux faire ça :

private static final String PATTERN_ENTIER = "\\d+"; // au moins un chiffre (d pour digit)

public static int convertStringToInt(String string) {
int i = 0;
if (string != null && string.matches(PATTERN_ENTIER)) {
i = Integer.parseInt(string);
}
return i;
}
0
Utilisateur anonyme
17 déc. 2009 à 01:10
Salut,

Ou encore:

SOLUTION 3

 public static int convertStringToInt(String string)
      {
         int i = 0; 
         
         final String PATTERN_ENTIER = "\\d+"; // au moins un chiffre (d pour digit)
         
         //if (string != null && !"".equals(string))
         //{
         
         try
         {
            i = Integer.parseInt(string);
         }
             catch (NumberFormatException nfe)
            {
               if(string == null)
                  System.out.println("ABANDON  " + nfe.toString()); // cas par ex. de saisie
                                             // en utilisant JOptionPane.showInputDialog(...
               							         // et clique sur le bouton  pour abandonner
               										// la saisie.
               else
                  if (!string.matches(PATTERN_ENTIER))
                     System.out.println("NON NUMÉRIQUE  " + nfe.toString()); // c'est la même exception...
                  else
                     System.out.println("TROP LONG  " + nfe.toString());
               //i = 0; // par exemple
            }
         //}
      
         return i;
      }


Cordialement,

...\ Dan /...
0
Rejoignez-nous