Java 1.7运行时错误
我正在尝试解决此网站上的问题:。但我的部分代码抛出运行时错误:Java 1.7运行时错误,java,runtime-error,Java,Runtime Error,我正在尝试解决此网站上的问题:。但我的部分代码抛出运行时错误: class Main { static String ReadLn (int maxLg) // utility function to read from stdin { byte lin[] = new byte [maxLg]; int lg = 0, car = -1; String line = ""; try { while (lg < maxLg)
class Main {
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main(String[] args){
Main jollyJumper = new Main();
jollyJumper.start();
}
public void start(){
String input;
while((input = ReadLn(3000)) != null){
System.out.println(answer(input));
}
}
public String answer(String line){
// The error comes from this function !!!
String[] items = line.split(" ");
int[] array;
try {
array = new int[items.length - 1];
}
catch(NegativeArraySizeException e){
return "Not jolly";
}
for(int i = 0; i < array.length; i++){
array[i] = Integer.parseInt(items[i +1]);
}
return "Jolly";
}
}
主类{
静态字符串ReadLn(int-maxLg)//从stdin读取的实用函数
{
字节lin[]=新字节[maxLg];
int lg=0,car=-1;
字符串行=”;
尝试
{
while(lg
但是评判系统不会告诉我错误在哪里,在我的电脑上一切正常。有什么想法吗?这段代码崩溃的一个简单原因是一行中包含多个空格的输入行。通过修剪数组和拆分一系列空白字符来防止这种情况
String[] items = line.split("\\s+").trim();
实际上,一行中的两个空格会导致转换错误:
java.lang.NumberFormatException: For input string: ""
在Q中进行大量编辑后编辑
另一个错误(根据规范)是使用
byte[3000]
用于读取可包含计数和最多3000个整数的行。即使是一半的整数也需要3000多个字符或字节。只需使用扫描仪
最后,方法
答案并不能解决问题。它只是转换数字,但所需的处理(检查连续数字之间的差异不超过一个限制,从1到限制的所有值都在那里)没有完成。请向我们展示stacktraceThere没有足够的代码来告诉您的程序哪里出了故障。而且它很容易崩溃。请提供isJolly()。@user:该网站的在线评判系统不提供stacktrace,它只提供字符串“Runtime Error”。