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; }
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; }
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; }