CodeGym /Java Blog /Random-IT /NumberFormatException in Java
John Squirrels
Livello 41
San Francisco

NumberFormatException in Java

Pubblicato nel gruppo Random-IT

Cos'è NumberFormatException in Java?

"L'eccezione NumberFormatException viene lanciata alla conversione di una stringa non numerica in un numero."
In poche parole, se una stringa non contiene solo dati numerici e provi a convertirla in un numero, incontrerai questa eccezione. Questo può essere interpretato anche in un altro modo. L'eccezione viene utilizzata come indicatore se la conversione di String in un numero non è possibile. Il numero può essere un numero intero, float o decimale secondo le vostre esigenze. Ad esempio, se la stringa di input contiene tutte le lettere, caratteri alfanumerici o caratteri speciali e si tenta di eseguire una conversione da String a Integer , verrà generata un'eccezione NumberFormatException.

Esempio

Diamo un'occhiata a un semplice esempio per capirlo.

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

Produzione

Integer.pareseInt(2550) = 2550 NumberFormatException generata! Per la stringa di input: "255.0" NumberFormatException lanciata! Per la stringa di input: "Wed Aug 11 08:18:21 PKT 2021" NumberFormatException generata! Per la stringa di input: "1 2 3" NumberFormatException lanciata! Per la stringa di input: "Lubaina Khan"

Conclusione

Ci auguriamo che tu capisca perché NumberFormatException si presenta in Java. Se hai compreso la causa principale, puoi sempre identificare il motivo e correggerlo. Se ancora non ti è chiaro questo concetto, fai più errori e imparerai i perché e i come di questo problema. Fino ad allora continua a crescere e continua ad imparare.
Commenti
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION