[BufferedReader输入,而从文件读取时,在while循环中始终为空

问题描述 投票:0回答:1

我正在尝试读取文本文件并创建一个数组。使用出现在第一行的文件的前两个值设置数组。然后将文件中所有剩余的项目添加到数组中。由于某种原因,我的数组也始终为null。为了使其正常运行,需要进行哪些更正?

public static Grid createGrid(String filename) throws IOException {
        BufferedReader input;
        String inputLine;
        char[][] grid;
        Grid currentGrid;
        int currentLine = 0;
        try{
            input = new BufferedReader(new FileReader(filename));
            inputLine = input.readLine();
            String[] tokens = inputLine.split(" ");
            if(tokens.length != 2){
                throw new IllegalArgumentException();
            }
            int height = Integer.parseInt(tokens[0]);
            int width = Integer.parseInt(tokens[1]);
            grid = new char[height][width];
            while(inputLine != null){ // Giving me (Condition 'inputLine != null' is always 'true') in ide
                System.out.println(inputLine);
                inputLine = input.readLine();
                for(int i = 0; i < inputLine.length() - 1; i++){
                    char currentGem = inputLine.charAt(i);
                    grid[currentLine][i] = currentGem;
                }
                currentLine++;
            }
            input.close();
            currentGrid = new Grid(grid);
        }
        catch(IOException e){
            System.out.println(e.getMessage());
            currentGrid = null;
        }
        return currentGrid; // Giving me (Value 'currentGrid' is always 'null') in ide
    }
java arrays file-io bufferedreader
1个回答
0
投票

使用您的代码...

while(inputLine!= null)

...始终为true,因为inputLine是您的String,并且在运行此检查时它将始终具有值。原因是因为一旦您将null读入inputLine(到达文件末尾),而不是运行循环检查,则在尝试运行下一行时会抛出NullPointerException代码... for(int i = 0; i < inputLine.length() - 1; i++)。反过来,这将导致currentGrid始终为null,因为此时它仍被声明为null

要解决此问题,您只需在inputLine = input.readLine();之前添加while,然后将while中的同一行移至while的末尾,如下所示...

public static Grid createGrid(String filename) throws IOException {
    Grid currentGrid = null;
    try {
        BufferedReader input = new BufferedReader(new FileReader(filename));
        String inputLine = input.readLine();
        String[] tokens = inputLine.split(" ");
        if(tokens.length != 2){
            throw new IllegalArgumentException();
        }
        int height = Integer.parseInt(tokens[0]);
        int width = Integer.parseInt(tokens[1]);
        char[][] grid = new char[height][width];
        int currentLine = 0;
        inputLine = input.readLine(); // added before while
        while(inputLine != null) {
            System.out.println(inputLine);
            for(int i = 0; i < inputLine.length() - 1; i++){
                char currentGem = inputLine.charAt(i);
                grid[currentLine][i] = currentGem;
            }
            currentLine++;
            inputLine = input.readLine(); // last line in while
        }
        input.close();
        currentGrid = new Grid(grid);
    }
    catch(IOException e) {
        System.out.println(e.getMessage());
        currentGrid = null;
    }
    return currentGrid; 
}

[哦,您还会注意到我搬了您的声明。您无需在BufferedReader input块外声明String inputLinechar[][] gridtry { ... } catch。另外,您只需要在需要之前声明int currentLine = 0;(这是一个好习惯)。

© www.soinside.com 2019 - 2024. All rights reserved.