CodeGym /جاوا بلاگ /Random-UR /جاوا میں NumberFormatException
John Squirrels
سطح
San Francisco

جاوا میں NumberFormatException

گروپ میں شائع ہوا۔

جاوا میں NumberFormatException کیا ہے؟

"NumberFormatException کو غیر عددی سٹرنگ کے نمبر میں تبدیل کرنے پر پھینک دیا جاتا ہے۔"
سیدھے الفاظ میں، اگر ایک String میں صرف عددی ڈیٹا نہیں ہے اور آپ اسے نمبر میں تبدیل کرنے کی کوشش کرتے ہیں، تو آپ کو اس استثناء کا سامنا کرنا پڑے گا۔ اس کی تشریح ایک اور طریقے سے بھی کی جا سکتی ہے۔ استثناء کو اشارے کے طور پر استعمال کیا جاتا ہے اگر String کو کسی نمبر میں تبدیل کرنا ممکن نہ ہو۔ نمبر آپ کی ضروریات کے مطابق ایک عدد، فلوٹ یا اعشاریہ ہو سکتا ہے۔ مثال کے طور پر، اگر ان پٹ سٹرنگ میں تمام حروف، الفا عددی حروف یا خصوصی حروف شامل ہیں اور آپ String سے Integer میں تبدیلی کی کوشش کرتے ہیں تو ایک NumberFormatException پھینک دیا جائے گا۔

مثال

اس کو سمجھنے کے لیے ایک سادہ سی مثال دیکھتے ہیں۔
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());
		}
	}
}

آؤٹ پٹ

Integer.pareseInt(2550) = 2550 NumberFormatException پھینک دیا گیا! ان پٹ سٹرنگ کے لیے: "255.0" NumberFormatException پھینک دیا گیا! ان پٹ سٹرنگ کے لیے: "بدھ 11 اگست 08:18:21 PKT 2021" NumberFormatException پھینک دیا گیا! ان پٹ سٹرنگ کے لیے: "1 2 3" NumberFormatException پھینک دیا گیا! ان پٹ سٹرنگ کے لیے: "لبینہ خان"

نتیجہ

ہمیں امید ہے کہ آپ سمجھ گئے ہوں گے کہ جاوا میں NumberFormatException کیوں پیدا ہوتا ہے۔ اگر آپ بنیادی وجہ کو سمجھ چکے ہیں، تو آپ ہمیشہ وجہ کی نشاندہی کر سکتے ہیں اور اسے درست کر سکتے ہیں۔ اگر آپ اب بھی اس تصور کے بارے میں واضح نہیں ہیں، تو مزید غلطیاں کریں اور آپ اس مسئلے کی وجوہات اور کیسے ہونے کے بارے میں جان لیں گے۔ تب تک بڑھتے رہیں اور سیکھتے رہیں۔
تبصرے
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION