Java 文件中的数据未读入数组

Java 文件中的数据未读入数组,java,Java,我正在尝试将2D数组读入文件。在编译时,它表示readFile,Scanner变量可能未初始化(我知道这是因为初始化在try-catch块中)。但是,当我将readFile设置为null时,它会设法计算行数,但会跳过其他所有内容 我是否需要关闭并重新制作扫描仪和文件,以便实际读取数据?还是我遗漏了另一个错误 另外,我知道在下面的代码示例中没有实际读入2d数组的代码,但我正在尝试确保它在执行任何其他操作之前正确读入 //2D table example I'm trying to read in

我正在尝试将2D数组读入文件。在编译时,它表示readFile,Scanner变量可能未初始化(我知道这是因为初始化在try-catch块中)。但是,当我将readFile设置为null时,它会设法计算行数,但会跳过其他所有内容

我是否需要关闭并重新制作扫描仪和文件,以便实际读取数据?还是我遗漏了另一个错误

另外,我知道在下面的代码示例中没有实际读入2d数组的代码,但我正在尝试确保它在执行任何其他操作之前正确读入

//2D table example I'm trying to read in
0 1 1 0
1 0 0 0
1 0 0 1
0 0 1 0

boolean[][] relationTable;
Scanner keyboard = new Scanner(System.in);
String fileName;
File relationFile;
Scanner readFile;
boolean error = false;
System.out.print("Please enter the name of the table file: ");
        do{
            fileName = keyboard.next();
            try
            {
                relationFile = new File(fileName);
                readFile = new Scanner(relationFile);
                error = false;
            }
            catch(FileNotFoundException fnfe)
            {
                System.out.println("File was not found. Please enter a new file name:");
                error = true;
            }

        }while (error == true);

        //finds number of lines correctly
        int count = 0;
        while (readFile.hasNextLine()) 
        {
            count++;
            readFile.nextLine();  //notes the error here when not initialized
        }
        System.out.println(count); //using the example table above it prints out 4

        relationTable = new boolean[count][count];
        int i = 0, j = 0, temp = 0;

        //doesn't appear to do this section at all.
        String[] lines= new String[count]; 
        while (readFile.hasNextLine())
        {
            lines[i] = readFile.nextLine();
            System.out.println(lines[i]);
            i++;

        }

问题是您已经浏览了文件中的所有行

while (readFile.hasNextLine()) 
{
    count++;
    readFile.nextLine();  //notes the error here when not initialized
}
在这一点之后,您的readFile对象没有更多的行了。所以当你到达

String[] lines= new String[count]; 
while (readFile.hasNextLine())
{
    lines[i] = readFile.nextLine();
    System.out.println(lines[i]);
    i++;
}
已经没有线了。因此,为了解决您的问题,您必须在第二次读取之前再次读取该文件

relationFile = new File(fileName);
readFile = new Scanner(relationFile);

所以编译时会出现什么错误?嗯,一旦扫描到文件的末尾,扫描器就位于该位置。如果您不允许使用
列表
,并且需要提前计算行数,则需要重新关闭和打开。感谢您的帮助!这可能是一个愚蠢的问题,但如果我将其更改为arraylist,我可以使用与使用常规数组相同的方法来使用.split()和parseInt吗?
    // finds number of lines correctly
    int count = 0;
    List<String> lines = new ArrayList<String>();// use arrayList instead of arrays if you dont know the number of lines like we have here.
    while (readFile.hasNextLine()) {
        count++;
        lines.add(readFile.nextLine()); // notes the error here when not
                                        // initialized
    }
    System.out.println(count); // using the example table above it prints
                               // out 4

    relationTable = new boolean[count][count];
    int i = 0, j = 0, temp = 0;
    // here is a loop that works even though its not needed, we could have added the print statement in the loop above.   
    for (int x = 0; x < count; x++) {
        System.out.println(lines.get(x));

    }