ஜாவாவில் 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 தூக்கி எறியப்பட்டது! உள்ளீட்டு சரத்திற்கு: "லுபைனா கான்"
GO TO FULL VERSION