我很難弄清楚爲什麼只有我的部分代碼工作。我從一個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