繁体   English   中英

如何将 txt 文件转换为 Java 中的二维数组?

[英]How to convert txt file into 2d array in Java?

这是我第一次使用 arrays,尤其是 Java 中的文件,我遇到了如何将 output 中的数据从文件转换为二维数组的问题。

所以在这里我试图使用这些数据为游戏构建一个 map 并将其保存为二维数组。 怎么做?

我在文件中的数据:

8
2 0 0 1 1 0 0 0
0 0 1 0 0 1 0 0
0 0 0 0 0 0 0 0
1 0 1 0 0 1 0 1
1 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 1 0 0 1 0 0
0 0 0 1 1 0 0 0

我得到的错误:

文件文件 表达式的类型必须是数组类型,但它解析为文件


File file = new File("map1.txt");
        private final char[] allowed = {'0', '1', '2'};
        public List<Position> pPos = new ArrayList<>();
        Map(Scanner scan) throws InvalidMapException {
            List<String> strings = new ArrayList<>();
            int size = 8;
            boolean isValid = false;
            for(int i = 0; i < size; i++) {
                strings.add(size);
                for(char ch : allowed) {
                    if(strings.get(strings.size() - 1).charAt(0) == ch) {
                        isValid = true;
                    }
    
                }
            
                if(!isValid) {
                    throw new InvalidMapException("Illegal character");
                }
    
            }
                isValid = false;
                if(strings.size() != size) {
                    throw new InvalidMapException("Map error");
                }
            isValid = false;
            int chCounter = 0;
            int xCounter = 0;
            int yCounter = 0;
            for(String string : strings) {
                for(char ch : strings.toCharArray()) {
                    if(ch == ' ') {
                        continue;
                    }
                    if(ch == '2') {
                        pPos.add(new Position(xCounter, yCounter));
    
                    }
                    chCounter++;
                    for(char aCh: allowed) {
                        if(ch == allowed) {
                            isValid = true;
                            break; 
                        }
    
                    }
                    if(!isValid) {
                        throw new InvalidMapException("character error");
                    }
                    isValid = false;
                    chCounter++;
    
                }
                if(chCounter != strings.size()) {
                    System.out.println(chCounter + " " + strings.size());
                    throw new InvalidMapException("size error");
                }
                chCounter = 0;
                yCounter++;
                xCounter = 0;
    
            }
            file = new char[strings.size()][strings.size()];
            
            int i = 0; int j = 0;
            for(String string : strings) {
                for(char ch : string.toCharArray()) {
                    if(ch != ' ') {
                        file[i][j] = ch; //Here is the error.
                        j++;
                    }
                }
            }
    
        }

你这样做非常复杂。

读取文件的所有行:

List<String> lines = new ArrayList<>();
BufferedReader br = null;

try {
    br = new BufferedReader(new FileReader(fileName));
    String line;
        while ((line = br.readLine()) != null) {
            lines.add(line);
        }
}
catch (IOException e) {
        e.printStackTrace();
} finally {
    if (br != null) {
        br.close();
    }
}

然后用 String.split(" ") 分割行。 您将得到一个每行包含所有数字的数组。 将它们存储在列表中。

之后,您可以从项目中创建一个二维数组。

嗨,变量文件的类型为 java.io.File。 您正在尝试为其分配一个 char 数组,这是不可能的。 你需要使用这样的东西

char[][] charArray = new char[strings.size()][strings.size()];

无论如何,我建议您遵循 David 建议的方法,而不是您发布的复杂代码。

尽量保持解决方案简单易读,请试试这个 -

    File file = new File("map1.txt");
    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        String line = reader.readLine();
        int size = Integer.parseInt(line);
        int[][] array = new int[size][size];
        int i = 0;
        while ((line = reader.readLine()) != null) {
            int j = 0;
            String[] lineValues = line.split(" ");
            for (String value : lineValues) {
                array[i][j] = Integer.parseInt(value);
                j++;
            }
            i++;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM