我很难弄清楚为什么只有我的部分代码工作。我从一个txt文件中提取数据,以回答用户提示回答的问题。这是我的错误:泰坦尼克号统计应用程序
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at TestTitanic.filler(TestTitanic.java:13) at TestTitanic.main(TestTitanic.java:41)
C:\Users\Jznica Sabatini\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53:
Java returned: 1 BUILD FAILED (total time: 0 seconds)
这里是第13,41行,我在53,即使我不知道是什么错误。
line 13 String content = new Scanner(new File(args[0]))
line 41 runner.filler(args);
所以我有一个测试类(其中,在上述的例子是从,钛酸类,并且虽然可能是不必要的,一个子类的四价钛与延伸方法,但它的所有编译,它只是当我在cmd行编译,测试类与titanic.txt文件一起运行,因为cmd ln仅在用户提示响应实际打印时提出7个问题中的3个
以下是我的TestTitanic类,我使用的是netbeans并允许通过建议来改变它,现在我甚至不让我的cmd ln来打印问题或提示用户输入他们的回应。在net beans中它说我的测试类是我的错误所在,但是给定一些的在我的CMD行错误的行我知道这是我原来的泰坦尼克父类,但现在我会开始与较小的错误。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestTitanic {
// Method that builds a 2D array from the txt file
public void filler(String[] args) {
// Try-catch is needed for passing args to method
try {
// Turns the txt file into one large string
String content = new Scanner(new File(args[0]));
.useDelimiter("\\Z") // To the end of the file
.next();
String[] rows = content.split("\n"); // Breaks up by new line
// Creates a 2D array as "long" as the input file
String[][] titanic = new String[rows.length][];
// Fills the 2D array
for (int i = 0; i < rows.length; i++) {
titanic[i] = rows[i].split("\\t");
}
// Creates a new Titanic object
Titanic titanicObject = new Titanic(titanic);
// Calls the passToMenu method from Titanic class
titanicObject.passToMenu();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
public static void main(String[] args) {
// Create an object from this class
TestTitanic runner = new TestTitanic();
// Calls the program method to fill the array
Runnable runnable = new Runnable() {
@Override
public void run() {
runner.filler(args);
}
};
}
}
第13行最后没有分号!此外,请附上您的代码行
and
。 –为什么你要在主线程中创建一个新线程?它似乎不像你打算多线程。另外:“......我正在使用netbeans,并允许通过这些建议来改变它”< - 这就是疯狂,只有在你理解了它们的情况下才会采取建议。 –
所以我修复了这个问题,回到了我对一个问题的回答中输入的原始问题,并根据我选择回答的问题接收多个outofbounds错误。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Titanic.perishedPercent(Titanic.java:40) //line 40 if (row[1].equals("0")) {
这只是其中一个错误的众多例子之一,但如果您希望看到更多的话,我会非常乐意提供更好的理解。 – 3monkeys1gorilla