NumberFormatException 是 java 中的未经检查的异常,当用户尝试将字符串转换为数值时发生。 NumberFormatException 是 java 中的内置类,定义在 Java.lang.NumberFormatException 包中。 IllegalArgumentException 类是 NumberFormatException 的超类,因为它是未经检查的异常,因此不强制处理和声明它。 NumberFormatException 实际上是由 parseXXX() 函数抛出的,当函数无法将字符串转换或格式化(convert)为整数、浮点、双精度等数字值时,因为输入字符串的格式非法且不合适。例如,在 Java 程序中,有时用户输入通过命令行参数作为字符串形式的文本字段接受。并且要在某些算术运算中使用此字符串,应首先使用包装类的 parseXXX() 函数解析或转换为数据类型(特定数字类型)。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
NumberFormatException 类的层次结构是:
对象 ->可抛出 -> 异常 ->RuntimeException ->NumberFormatException。
语法
以下是java.io的声明。 PrintWriter 类。
public class NumberFormatException extends IllegalArgumentException implements Serializable { // Constructors of the NumberFormatException class }
上面是NumberFormatException的语法,在这里它被扩展为:
IllegalArgumentException class and implements Serializable
当尝试将字符串转换为数字时,会发生 NumberFormatException。这种类型的转换是通过使用 Integer.parseInt()、Float.parseFloat() 等函数来执行的。假设我们调用 Integer.parseInt(s) 函数,其中 s 是 String 类型,其值为字符串“30”,因此该函数将字符串值正确转换为 int 30。但是发生了什么?如果 s 的值被假定为“三十”(这是非法的),则该函数将失败并引发异常:NumberFormatException。为了处理这个异常,我们可以为其编写catch块;如果不处理异常,程序就会崩溃。
抛出 NumberFormatException 的原因有多种,如下所示 –
以下是实施示例:
接下来,我们编写java代码来更清楚地理解NumberFormatException,通过以下示例,我们使用PrintWriter类构造函数创建一个PrintWriter对象,并传递要写入的文件名,如下所示 –
代码:
//package p1; import java.util.Scanner; public class Demo { public static void main( String[] arg) { int age; Scanner sc = new Scanner(System.in); System.out.println("Please Enter your age : "); //throws Exception as if the input string is of illegal format for parsing as it as null or alphanumeric. age = Integer.parseInt(sc.next()); System.out.println("Your age is : " +age); } }
输出:
当用户输入“25+”时,上述代码的输出为:
当用户输入格式不正确的字符串“25F”时,输出为:
当用户输入字符串“Twenty Five”时,输出为:
当用户输入字符串“null”时,输出为:
当用户输入浮点型“40.78”字符串值时,输出为:
When the user enters a string “25”, which is a valid string. The output is:
Explanation: As in the above code, the age value is accepted from the user in string format, which further converts into the integer format by using the Integer.parseInt(sc.next()) function. While the user an illegal input string or not a well-formatted string, the NumberFormatException occurs, it throws, and the program terminates unsuccessfully. So to provide the valid string, it should be taken care that an input string should not be null, check an argument string matches the type of the conversion function used and also check if any unnecessary spaces are available; if yes, then trim it and so all care must be taken.
Next, we write the java code to understand the NumberFormatException where we generate the NumberFormatException and handle it by using the try-catch block in the program, as below –
Code:
//package p1; import java.util.Scanner; public class Demo { public static void main( String[] arg) { int age; Scanner sc = new Scanner(System.in); try { System.out.println("Please Enter your age : "); //throws Exception as if the input string is of illegal format for parsing as it as null or alphanumeric. age = Integer.parseInt(sc.next()); System.out.println("Your age is : " +age); } catch(NumberFormatException e) { System.out.println("The NumberFormatExceptionoccure."); System.out.println("Please enter the valid age."); } } }
When the user enters a string “23F”, the output is:
When a user enters a string “23”, the output is:
Explanation: As in the above code try-catch block is used. It is always a good practice to enclose lines of code that can throw an exception in a try-catch block by which it handles the NumberFormatException and prevents it from generating the Exception.
The NumberFormatException in java is an unchecked exception that occurs when a not well-formatted string is trying to converts into a numeric value by using the parseXXX() functions. The NumberFormatException is a built-in class in which is defined in the Java.lang.NumberFormatException package.
以上是Java 中的 NumberFormatException的详细内容。更多信息请关注PHP中文网其他相关文章!