简体   繁体   English

如何在java中拆分数字文件?

[英]How do I split a file of numbers in java?

I am trying to convert a file full of numbers (9x9) into an a readable array based off columns and rows.我正在尝试将一个充满数字 (9x9) 的文件转换为基于列和行的可读数组。 It is for a sudoku game builder.它适用于数独游戏制作者。 The file reads as so:该文件是这样写的:

0,0,2,9,8,0,5,0,0
4,0,0,0,7,0,0,1,3
0,3,9,6,0,4,0,7,0
2,0,0,0,5,6,4,0,0
8,4,0,3,0,0,2,0,1
9,0,7,0,0,1,0,8,6
6,0,0,7,0,5,1,3,0
0,9,1,4,0,0,0,0,5
0,2,0,0,3,0,6,0,8

Where I'm using the "0's" as blank spaces.我使用“0”作为空格的地方。 I want to split the numbers at the commas so for the first row I want it to read: gameBoard[0] = [0,0,2,9,8,0,5,0,0] or as gameBoard[0][4] = [9].我想在逗号处拆分数字,因此对于第一行,我希望它读取:gameBoard[0] = [0,0,2,9,8,0,5,0,0] 或作为 gameBoard[0] [4] = [9]。 The array I'm using is gameBoard[][].我使用的数组是 gameBoard[][]。

Some people told me it would be helpful to include some code I tried so here it is:有些人告诉我,包含一些我尝试过的代码会很有帮助,所以这里是:

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                gameBoard[i][j] = game.split(",");
            }
        }

This section of code is within a method that is called through a constructor.这部分代码位于通过构造函数调用的方法中。

A little start-help to a new contributor ..对新贡献者的一点帮助..

    String[][] gameBoard = new String[9][9];
    File f = new File("zz_play9.txt");               
    try {
        BufferedReader b = new BufferedReader(new FileReader(f));

        String readLine = "";        
        int linecounter =0;
        while ((readLine = b.readLine()) != null) {
            System.out.println(readLine);
            gameBoard[linecounter++] = readLine.split(",");
        }
        b.close();
    } catch (IOException ex) {
        Logger.getLogger(zz_play9.class.getName()).log(Level.SEVERE, null, ex);
    } 

I have entered your start-data into a file called zz_play9.txt.我已将您的开始数据输入到名为 zz_play9.txt 的文件中。

By running the program you get:通过运行该程序,您将获得:

0,0,2,9,8,0,5,0,0
4,0,0,0,7,0,0,1,3
0,3,9,6,0,4,0,7,0
2,0,0,0,5,6,4,0,0
8,4,0,3,0,0,2,0,1
9,0,7,0,0,1,0,8,6
6,0,0,7,0,5,1,3,0
0,9,1,4,0,0,0,0,5
0,2,0,0,3,0,6,0,8

and the array gameBoard[][] holds the desired data.数组 gameBoard[][] 保存所需的数据。 Have fun.玩得开心。

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

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