首页  >  文章  >  Java  >  Java 中的 NumberFormatException

Java 中的 NumberFormatException

WBOY
WBOY原创
2024-08-30 16:14:14657浏览

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 在 Java 中如何工作?

当尝试将字符串转换为数字时,会发生 NumberFormatException。这种类型的转换是通过使用 Integer.parseInt()、Float.parseFloat() 等函数来执行的。假设我们调用 Integer.parseInt(s) 函数,其中 s 是 String 类型,其值为字符串“30”,因此该函数将字符串值正确转换为 int 30。但是发生了什么?如果 s 的值被假定为“三十”(这是非法的),则该函数将失败并引发异常:NumberFormatException。为了处理这个异常,我们可以为其编写catch块;如果不处理异常,程序就会崩溃。

抛出 NumberFormatException 的原因有多种,如下所示 –

  • 提供的要转换的字符串可能为空。例如:Integer.parseInt(null);
  • 提供的字符串长度可能为零。例如:Integer.parseInt(“”);
  • 提供的字符串可能没有数字字符。例如:Integer.parseInt(“三十”);
  • 提供的字符串可能不代表整数值。例如:Integer.parseInt(“FG67”);
  • T 提供的字符串可能为空。例如:Integer.parseInt(“”);
  • 提供的字符串可能尾随空格。例如:Integer.parseInt(“785”);
  • 提供的字符串可能有前导空格。例如:Integer.parseInt(“785”);
  • 提供的字符串可能包含字母数字。例如:Long.parseLong(“F5”);
  • 提供的字符串可能是支持的数据类型超出范围。例如:Integer.parseInt(“139”);
  • 提供的字符串和用于转换的函数可能是不同的数据类型。Ex :Integer.parseInt(“3.56”);

构造函数

  • NumberFormatException(): 此构造函数创建 NumberFormatException,但没有详细的特定消息。
  • NumberFormatException(String s): 此构造函数创建包含特定消息详细信息的 NumberFormatException。

在 Java 中实现 NumberFormatException 的示例

以下是实施示例:

示例#1

接下来,我们编写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+”时,上述代码的输出为:

Java 中的 NumberFormatException

当用户输入格式不正确的字符串“25F”时,输出为:

Java 中的 NumberFormatException

当用户输入字符串“Twenty Five”时,输出为:

Java 中的 NumberFormatException

当用户输入字符串“null”时,输出为:

Java 中的 NumberFormatException

当用户输入浮点型“40.78”字符串值时,输出为:

Java 中的 NumberFormatException

When the user enters a string “25”, which is a valid string. The output is:

Java 中的 NumberFormatException

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.

Example #2

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:

Java 中的 NumberFormatException

When a user enters a string “23”, the output is:

Java 中的 NumberFormatException

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.

Conclusion

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn