线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18660175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
提问by mihir S
Original Question
原始问题
For the following small code I'm getting the error...
对于以下小代码,我收到错误...
import java.io.*;
class test
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i;
System.out.println("Enter no of processes ");
int no_of_process=Integer.parseInt(br.readLine());
int process[]=new int[no_of_process];
System.out.println("Enter the values");
for(i=0;i<no_of_process;i++)
process[i]=Integer.parseInt(br.readLine());
for(i=0;i<no_of_process;i++)
System.out.println(process[i]);
}
}
Input:
输入:
Enter no of processes
5
Enter the values
1
2
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at test.main(test.java:17)
Process completed.
I think I have written the code properly and proper integer input is also given. How do I get rid of the above error without using any explicit exception handling statements?
我想我已经正确地编写了代码并且还给出了正确的整数输入。如何在不使用任何显式异常处理语句的情况下摆脱上述错误?
Further Question:
进一步的问题:
Thanks guys for your answers...it is working. But there is one new question in my mind.
谢谢你们的回答......它正在工作。但是我的脑海里出现了一个新问题。
I tried the following modification in the code and executed it. To my surprise, input was accepted properly without any run time error.
我在代码中尝试了以下修改并执行了它。令我惊讶的是,输入被正确接受,没有任何运行时错误。
for(i=0;i<no_of_process;i++)
{
System.out.println(Write anything or even keep it blank);
process[i]=Integer.parseInt(br.readLine());
}
By adding just one Print statement before (or even after) the input statement in the for loop, my program worked correctly and no exception was thrown while giving input.
通过在 for 循环中的输入语句之前(甚至之后)仅添加一个 Print 语句,我的程序可以正常工作并且在提供输入时没有抛出异常。
Can you guys explain the reason behind this?
大家能解释一下这背后的原因吗?
Again if I remove the Print statement from there, the same error gets repeated. I am really confused about the reason behind this. Please help.
同样,如果我从那里删除 Print 语句,则会重复出现相同的错误。我真的很困惑这背后的原因。请帮忙。
回答by StormeHawke
Without any error handling statements? check to see if br.readLine() is returning "" before you attempt to parse it, like so:
没有任何错误处理语句?在尝试解析之前检查 br.readLine() 是否返回 "",如下所示:
String line = br.readLine();
if(!String.isEmpty(line))
{
//Do stuff
}
else
{
//Do other stuff
}
回答by Philipp Sander
How to get rid of above error without using any explicit exception handling statements?
如何在不使用任何显式异常处理语句的情况下摆脱上述错误?
for (i = 0; i < no_of_process; i++) {
String input;
do {
input = br.readLine();
if (isInteger(input)) {
process[i]=Integer.parseInt(input);
} else {
//error handling here
System.err.println("you entered an invalid input");
}
} while(isInteger(input));
}
And isInteger
looks like this:
而且isInteger
是这样的:
public static boolean isInteger(String s)
{
try {
Integer.parseInt(s);
}
catch (Exception e) {
return false;
}
return true;
}
I think I have written properly and proper integer input is also given.
我想我写得正确,也给出了正确的整数输入。
I think not ;) i think you pressed the return with out typing anything
我认为不是 ;) 我认为你没有输入任何东西就按下了回车键
回答by ravi jadhav
Try this:
尝试这个:
import java.io.*;
public class test
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i;
System.out.println("Enter no of processes ");
try{
int no_of_process=Integer.parseInt(br.readLine());
int process[]=new int[no_of_process];
System.out.println("Enter the values");
for(i=0;i<no_of_process;i++)
process[i]=Integer.parseInt(br.readLine());
for(i=0;i<no_of_process;i++)
System.out.println(process[i]);
}
catch(NumberFormatException n)
{
System.out.println(n.getMessage());
}
}
}