文件中的数据未读入数组

时间:2015-11-11 18:53:36

标签: java

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

我是否需要关闭并重新制作扫描仪和文件内容才能实际读取数据?或者是否有其他错误,我在某处丢失?

另外,我知道实际读取2d数组的代码没有在下面的代码示例中完成,但我正在尝试确保它在我做其他任何事情之前正确读取。< / p>

//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++;

        }

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

    // 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;

您的错误在于您使用readFile.hasNextLine()进行循环,但该文件已被阅读且readFile.hasNextLine()始终返回false

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

    }