线程 "main" java.lang.NumberFormatException 中的异常:对于输入字符串:“”(我输入了数字,但它似乎读取了一个空字符串)
Exception in thread "main" java.lang.NumberFormatException: For input string: "" (I entered digits, but it seems to have read an empty string)
这部分有问题:
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String faaltu = cin.readLine();
String inp = cin.readLine();
String[] part = inp.split("\s");
for(int k = 0; k < part.length; k++)
{
System.out.println(part[k]);
}
obj.Smax = Integer.parseInt(part[0]);
我给出了以下输入:
2
4 12345
3 1234
完整代码如下:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codejam
{
Codejam(){};
static Codejam obj = new Codejam();
int totalStanding = 0;
int T;//no of test cases
int[] S;// no of people at each given shyness level
boolean[] standing;
int Smax;
int total = 0, newInv = 0;
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
obj.T = Integer.parseInt(cin.readLine());
for(int i = 0; i < obj.T; i++)
{
obj.populate();
obj.update();
while (obj.totalStanding < obj.total)
{
obj.newInv++;
obj.S[0]++;
obj.update();
}
System.out.println("Case #" + i + ": " + obj.newInv);
}
}
public void update()
{
for(int i = 0;i < obj.S.length; i++)
{
if ((totalStanding >= i) && (obj.standing[i] == false) )
{
obj.totalStanding += obj.S[i];
obj.standing[i] = true;
}
}
}
public void populate() throws IOException
{
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String faaltu = cin.readLine();
String inp = cin.readLine();
String[] part = inp.split("\s");
for(int k = 0; k < part.length; k++)
{
System.out.println(part[k]);
}
obj.Smax = Integer.parseInt(part[0]);
obj.S = new int[Smax + 1];
obj.standing = new boolean[Smax + 1];
for(int j = 0;j < part[1].length(); j++)
{
obj.S[j] = part[1].charAt(j) - '0';
obj.total += S[j];
}
}
}
出现异常
Exception in thread "main" java.lang.NumberFormatException:
For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Codejam.populate(Codejam.java:57)
at Codejam.main(Codejam.java:24)
请指出我哪里做错了
错误很明显,您试图将一个空字符串解析为一个数字,这引发了异常。
我们不知道您的输入是什么,但 part[0]
是导致错误的空字符串:
obj.Smax = Integer.parseInt(part[0]);
在我看来,您正试图从您的输入中读取太多行。
我怀疑您正在阅读文件末尾。
我建议您将 cin.readline ()
作为参数传递给您的 populate()
方法,这样您就不必打开另一个 reader。
您也不应该如此严重地转义您的拆分表达式,我认为它当前读取的是 "a backslash and an s" 而不是 "whitespace"。
这部分有问题:
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String faaltu = cin.readLine();
String inp = cin.readLine();
String[] part = inp.split("\s");
for(int k = 0; k < part.length; k++)
{
System.out.println(part[k]);
}
obj.Smax = Integer.parseInt(part[0]);
我给出了以下输入:
2
4 12345
3 1234
完整代码如下:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codejam
{
Codejam(){};
static Codejam obj = new Codejam();
int totalStanding = 0;
int T;//no of test cases
int[] S;// no of people at each given shyness level
boolean[] standing;
int Smax;
int total = 0, newInv = 0;
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
obj.T = Integer.parseInt(cin.readLine());
for(int i = 0; i < obj.T; i++)
{
obj.populate();
obj.update();
while (obj.totalStanding < obj.total)
{
obj.newInv++;
obj.S[0]++;
obj.update();
}
System.out.println("Case #" + i + ": " + obj.newInv);
}
}
public void update()
{
for(int i = 0;i < obj.S.length; i++)
{
if ((totalStanding >= i) && (obj.standing[i] == false) )
{
obj.totalStanding += obj.S[i];
obj.standing[i] = true;
}
}
}
public void populate() throws IOException
{
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String faaltu = cin.readLine();
String inp = cin.readLine();
String[] part = inp.split("\s");
for(int k = 0; k < part.length; k++)
{
System.out.println(part[k]);
}
obj.Smax = Integer.parseInt(part[0]);
obj.S = new int[Smax + 1];
obj.standing = new boolean[Smax + 1];
for(int j = 0;j < part[1].length(); j++)
{
obj.S[j] = part[1].charAt(j) - '0';
obj.total += S[j];
}
}
}
出现异常
Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Codejam.populate(Codejam.java:57) at Codejam.main(Codejam.java:24)
请指出我哪里做错了
错误很明显,您试图将一个空字符串解析为一个数字,这引发了异常。
我们不知道您的输入是什么,但 part[0]
是导致错误的空字符串:
obj.Smax = Integer.parseInt(part[0]);
在我看来,您正试图从您的输入中读取太多行。
我怀疑您正在阅读文件末尾。
我建议您将 cin.readline ()
作为参数传递给您的 populate()
方法,这样您就不必打开另一个 reader。
您也不应该如此严重地转义您的拆分表达式,我认为它当前读取的是 "a backslash and an s" 而不是 "whitespace"。