【发布时间】:2015-01-09 21:07:24
【问题描述】:
我正在运行这个程序,它从命令行输入 t(Desired Length),no of test
例(N)。但它给出了运行时错误
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index
out of range: -1
at java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:979)
at java.lang.StringBuilder.insert(StringBuilder.java:300)
at Test.main(Test.java:28)
我在访问错误索引的地方感到困惑。
import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
String line = br.readLine();
int N = Integer.parseInt(line);
String[] profiles = null;
for (int i = 0; i < N; i++) {
profiles = br.readLine().trim().split(" ");
}
for (int i = 0; i < N; i++) {
if (Integer.parseInt(profiles[0]) > t && Integer.parseInt(profiles[1]) == t ||
Integer.parseInt(profiles[0]) > t || Integer.parseInt(profiles[1]) > t
|| Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) > t)
{
System.out.println("Crop");
}
if (Integer.parseInt(profiles[0]) < t && Integer.parseInt(profiles[1]) < t) {
System.out.println("Upload next");
}
if (Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) == t) {
System.out.println("Accepted");
}
}
System.out.println("Hello World!");
}
}
【问题讨论】:
-
“我在哪里访问错误的索引”很容易回答:
Test.javaline 28 -
你的文件是哪一行?您可以添加评论以找到它吗?
-
您的代码的目标是什么?检查 N 字符串是否更短、等于或长于 t?
-
@Sam 它给了 System.out.println("Upload next")...如果那会在其他行给我一个错误,那么我很容易..调试但它在第 28 行给出-->System.out.println("Upload next").
-
@Giulio 我正在尝试 --> 第一行包含 Length=t。第二行包含 N,照片数量。以下 N 行,每行包含两个空格分隔的整数 Width 和 Height。
标签: java