CodeGym /Blog Java /Random-FR /NumberFormatException en Java
Auteur
Alex Vypirailenko
Java Developer at Toshiba Global Commerce Solutions

NumberFormatException en Java

Publié dans le groupe Random-FR

Qu'est-ce que NumberFormatException en Java ?

"Le NumberFormatException est lancé lors de la conversion d'une chaîne non numérique en un nombre."
En termes simples, si une chaîne ne contient pas uniquement des données numériques et que vous essayez de la convertir en nombre, vous rencontrerez cette exception. Cela peut aussi être interprété d'une autre manière. L'exception est utilisée comme indicateur si la conversion de String en nombre n'est pas possible. Le nombre peut être un nombre entier, flottant ou décimal selon vos besoins. Par exemple, si la chaîne d'entrée contient toutes les lettres, caractères alphanumériques ou caractères spéciaux et que vous essayez de tenter une conversion de String en Integer , une NumberFormatException sera levée.

Exemple

Prenons un exemple simple pour comprendre cela.

import java.util.Date;

public class NumberFormatExceptionTestDriver {

	public static void main(String[] args) {

		try {
			// a valid Integer number in the input String
			String inputString = "2550";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			// java.lang.NumberFormatException will be thrown if the  
			// input string can not be converted to a valid integer
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// a floating number in the input String
			// use Float.parseFloat(inputString) to avoid this exception
			String inputString = "255.0"; 
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			Date day = new Date();
			// date containing alpha-numeric data in the input string
			String inputString = day.toString();
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// numbers with spaces in the input string
			String inputString = "1 2 3";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));
		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}

		try {
			// all letters in the input string
			String inputString = "Lubaina Khan";
			System.out.println("Integer.pareseInt(" + inputString + ") = " + Integer.parseInt(inputString));

		} catch (NumberFormatException e) {
			System.out.println("\nNumberFormatException thrown! " + e.getMessage());
		}
	}
}

Sortir

Integer.pareseInt(2550) = 2550 NumberFormatException levée ! Pour la chaîne d'entrée : "255.0" NumberFormatException levée ! Pour la chaîne d'entrée : "Mer 11 août 08:18:21 PKT 2021" NumberFormatException levée ! Pour la chaîne d'entrée : "1 ​​2 3" NumberFormatException levée ! Pour la chaîne d'entrée : "Lubaina Khan"

Conclusion

Nous espérons que vous comprenez pourquoi NumberFormatException apparaît en Java. Si vous avez compris la cause première, vous pouvez toujours identifier la raison et y remédier. Si vous n'êtes toujours pas clair sur ce concept, faites plus d'erreurs et vous apprendrez le pourquoi et le comment de ce problème. Jusque-là, continuez à grandir et continuez à apprendre.
Commentaires
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION