CodeGym /జావా బ్లాగ్ /యాదృచ్ఛికంగా /జావాలో నంబర్‌ఫార్మాట్ మినహాయింపు
John Squirrels
స్థాయి
San Francisco

జావాలో నంబర్‌ఫార్మాట్ మినహాయింపు

సమూహంలో ప్రచురించబడింది

జావాలో NumberFormatException అంటే ఏమిటి?

"సంఖ్యేతర స్ట్రింగ్‌ను సంఖ్యగా మార్చినప్పుడు NumberFormatException విసిరివేయబడుతుంది."
సరళంగా చెప్పాలంటే, స్ట్రింగ్ సంఖ్యా డేటాను మాత్రమే కలిగి ఉండకపోతే మరియు మీరు దానిని సంఖ్యగా మార్చడానికి ప్రయత్నిస్తే, మీరు ఈ మినహాయింపును ఎదుర్కొంటారు. దీనిని మరో విధంగా కూడా అర్థం చేసుకోవచ్చు. స్ట్రింగ్‌ను సంఖ్యగా మార్చడం సాధ్యం కానట్లయితే మినహాయింపు సూచికగా ఉపయోగించబడుతుంది . మీ అవసరాలకు అనుగుణంగా సంఖ్య పూర్ణాంకం, ఫ్లోట్ లేదా దశాంశం కావచ్చు. ఉదాహరణకు, ఇన్‌పుట్ స్ట్రింగ్‌లో అన్ని అక్షరాలు, ఆల్ఫా-న్యూమరిక్ అక్షరాలు లేదా ప్రత్యేక అక్షరాలు ఉంటే మరియు మీరు స్ట్రింగ్ నుండి పూర్ణాంకానికి మార్చడానికి ప్రయత్నించినట్లయితే, 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