CodeGym /Blog Java /Random-PL /NumberFormatException w Javie
Autor
Alex Vypirailenko
Java Developer at Toshiba Global Commerce Solutions

NumberFormatException w Javie

Opublikowano w grupie Random-PL

Co to jest wyjątek NumberFormatException w Javie?

„Wyjątek NumberFormatException jest generowany podczas konwersji ciągu nienumerycznego na liczbę”.
Mówiąc najprościej, jeśli łańcuch nie zawiera tylko danych liczbowych i spróbujesz przekonwertować go na liczbę, napotkasz ten wyjątek. Można to też zinterpretować w inny sposób. Wyjątek jest używany jako wskaźnik, jeśli konwersja ciągu znaków na liczbę nie jest możliwa. Liczba może być liczbą całkowitą, zmiennoprzecinkową lub dziesiętną, zgodnie z wymaganiami. Na przykład, jeśli ciąg wejściowy zawiera wszystkie litery, znaki alfanumeryczne lub znaki specjalne i spróbujesz dokonać konwersji z ciągu znaków na liczbę całkowitą , zostanie zgłoszony wyjątek NumberFormatException.

Przykład

Spójrzmy na prosty przykład, aby to zrozumieć.

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

Wyjście

Integer.pareseInt(2550) = 2550 Zgłoszono wyjątek NumberFormat! Dla ciągu wejściowego: „255.0” Zgłoszono wyjątek NumberFormatException! Dla ciągu wejściowego: „Wed Aug 11 08:18:21 PKT 2021” Zgłoszono wyjątek NumberFormatException! Dla ciągu wejściowego: „1 2 3” Zgłoszono wyjątek NumberFormatException! Dla ciągu wejściowego: „Lubaina Khan”

Wniosek

Mamy nadzieję, że rozumiesz, dlaczego wyjątek NumberFormatException pojawia się w Javie. Jeśli zrozumiałeś pierwotną przyczynę, zawsze możesz zidentyfikować przyczynę i ją naprawić. Jeśli nadal nie masz jasności co do tej koncepcji, popełnij więcej błędów, a poznasz przyczyny i sposoby tego problemu. Do tego czasu rozwijaj się i ucz.
Komentarze
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION