泰坦尼克统计应用

时间:2016-03-05 05:26:42

标签: java multidimensional-array indexoutofboundsexception filereader

我很难弄清楚为什么只有部分代码正常工作。我从txt文件中提取数据来回答用户提示回答的问题。这是我的错误:

  

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0在TestTitanic.filler(TestTitanic.java:13)的TestTitanic.main(TestTitanic.java:41)

     

C:\ Users \ Jznica Sabatini \ AppData \ Local \ NetBeans \ Cache \ 8.1 \ executor-snippets \ run.xml:53:

     

Java返回:1 BUILD FAILED(总时间:0秒)

以下是第13,41行,即使我不确定该错误是什么,我也会投入53行。

line 13    String content = new Scanner(new File(args[0]))
line 41    runner.filler(args);

所以我有一个测试类(其中上面的例子来自一个巨大的类,虽然可能是不必要的,一个带有extends方法的泰坦尼克号的子类,但它全部编译,它只是当我编译时cmd行和测试类使用titanic.txt文件运行,因为当用户提示实际打印的响应时,cmd ln只有7个问题中的3个。

以下是我的TestTitanic课程。我正在使用netbeans并允许通过建议更改它,现在我甚至没有让我的cmd ln打印问题或提示用户输入他们的响应。在net beans中,它说我的测试类是我的错误所在,但考虑到我的cmd行中出现的一些行错误,我知道这是我原来的Titanic父类,但是现在我将从较小的错误开始。

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);
            }
        };

    }
}

1 个答案:

答案 0 :(得分:0)

您可能没有为您的程序提供命令行参数,因此在

处获取 { "pid": "2", "product_name": "Some product one", "product_pic": "http://localhost/img/generic.png", "product_thumb": "", "product_description": "some long description", "product_rating": "0", "username": "john", "comment": "one of my favorites" }, { "pid": "2", "product_name": "Some product one", "product_pic": "http://localhost/img/generic.png", "product_thumb": "", "product_description": "some long description", "product_rating": "0", "username": "jane", "comment": "this was so cool" }

java.lang.ArrayIndexOutOfBoundsException: 1

由于您使用的是Netbeans IDE,因此可以将程序的命令行参数传递为:  using-command-line-arguments-in netbeans

pass-command-line-arguments-netbeans-ide