英文:
Why the stack trace contains repeat call to the parseInt() method?
问题
我使用JDK 11.0编译了以下代码:
```java
import java.util.Scanner;
public class inputWithScanner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name and roll number");
String name = input.next();
Integer rollNumber = Integer.parseInt(input.next());
input.close();
if (name.isEmpty())
System.out.println("oops! You didnt enter your name");
if (rollNumber < 0)
System.out.println("Enter a valid roll number");
System.out.println("Hello "+name+" Roll Number "+rollNumber);
}
并使用以下输入测试了该程序(对于rollNumber变量的混合输入是故意的):
Enter your name and roll number
Manraj 2018CSE1023
编译器引发了一个异常:NumberFormatException,堆栈跟踪如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: "2018CSE1023"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at inputWithScanner.main(inputWithScanner.java:7)
问题是:
为什么会重复调用parseInt()方法?
我尝试了相同的代码,使用valueOf()方法,并使用相同的输入执行了该程序。编译器引发了相同的异常,堆栈跟踪如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: "2018CSE1023"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.valueOf(Integer.java:983)
at inputWithScanner.main(inputWithScanner.java:7)
这表明valueOf()方法调用了parseInt()方法。但为什么parseInt()调用了自身?
<details>
<summary>英文:</summary>
I compiled the following code with JDK 11.0:
import java.util.Scanner;
public class inputWithScanner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name and roll number");
String name = input.next();
Integer rollNumber = Integer.parseInt(input.next());
input.close();
if (name.isEmpty())
System.out.println("oops! You didnt enter your name");
if (rollNumber < 0)
System.out.println("Enter a valid roll number");
System.out.println("Hello "+name+" Roll Number "+rollNumber);
and tested the program with the following input (mixed input for the rollNumber variable is deliberate):
Enter your name and roll number
Manraj 2018CSE1023
the compiler raises an exception: NumberFormatException with the stack trace as follows:
```Exception in thread "main" java.lang.NumberFormatException: For input string: "2018CSE1023"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at inputWithScanner.main(inputWithScanner.java:7)
The question is:
Why is there a repeated call for parseInt() method?
I tried same code with valueOf() method and executed the program with same input. The compiler raises the same exception, and stack trace is as follows:
Exception in thread "main" java.lang.NumberFormatException: For input string: "2018CSE1023"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.valueOf(Integer.java:983)
at inputWithScanner.main(inputWithScanner.java:7)
It shows that the valueOf() method calls parseInt() method. But Why parseInt() calls itself?
答案1
得分: 8
parseInt
被定义在这个链接中:
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s, 10); // 这是当前的第 770 行
}
然后在第 652 行(在内部调用parseInt
的地方),它抛出一个异常。此时堆栈跟踪中包括了这两个不同参数的parseInt
方法。
简而言之,这两个是不同的方法,一个是带一个参数的,它调用了带两个参数的方法。
valueOf
的定义在这个链接中:
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s, radix));
}
它直接调用了带两个参数的版本。
英文:
parseInt
is defined as
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10); // this is line 770 currently
}
Then at line 652 (inside that inner call to parseInt
) it throws an exception. At which point both the two-arguments parseInt
and the one-argument one are in the stack trace.
In short, those are two different methods, one with one arguments that calls one with two.
valueOf
is defined as
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s,radix));
}
Which calls the version with two arguments directly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论