将文本文件读入数组并进行修改

时间:2016-04-04 10:50:42

标签: java arrays

我想将普通文本文件读入双数组。文本文件采用以下格式(除了行之间没有空格):

5,1,1,1,2,1,3,1,1,2

5,4,4,5,7,10,3,2,1,4

3,1,1,1,2,2,3,1,1,2

6,8,8,1,3,4,3,7,1,4

4,1,1,3,2,1,3,1,1,2

然而,当将其读入数组时,我希望将前9个属性设置为十进制(如果本身(基本上除以10)),将最后一个数字(2或4)分离为#34;列"当它为2时,如果它是4,则将0输入到2D阵列的第10列,将1输入到第11列,反之亦然。 从本质上讲,我希望它的格式看起来像这样:

{0.5, 0.1, 0.1, 0.1, 0.2, 0.1, 0.3, 0.1, 0.1, 0, 1},

{0.5, 0.4, 0.4, 0.5, 0.7, 1, 0.3, 0.2, 0.1, 1, 0},

非常感谢任何帮助。

for (String str : values) {
    double str_double = Double.parseDouble(str);
    myDouble[x][y]=str_double/10;                               
    System.out.print(myDouble[x][y] + " ");
    y = y + 1;
}

1 个答案:

答案 0 :(得分:0)

As a good answer explains what have been done, I tried to comment the most important parts, including the file IO.

Please be aware of the difference between: result.length and result[row].length.

One gives you the length of the one dimension of the array, while the second gives you the dimension of the array contained in a specific position of the 2D array.

In Java, a multidimensional Array is defined as a "jagged" array, as the internal arrays can be of different sizes.

See if this answer clarifies what you need, and ask if you have any further questions.

EDIT: Corrected code to meet specifications from the question. Please run to verify the desired output.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Example {

    private static final String DELIMITER = ",";
    private static final int LINE_COUNT = 5;
    private static final int COL_COUNT = 11;

    public static void main (String[] argv) {

        //Create a reference to the file
        File incoming = new File("C:\\DEV\\input_file.txt");

        try {

            //Create the components to read the file
            FileReader fr = new FileReader(incoming);
            BufferedReader br = new BufferedReader(fr);
            String line = "";
            String[] splitLine;

            /*Can have your program read the file to count lines and assign proper size instead of hardcode*/
            double[][] result = new double[LINE_COUNT][COL_COUNT];

            //Count the line being processed
            int row = 0;

            //This reads each line of the file
            while ((line = br.readLine()) != null) {

                //Splits the line around delimiter (,)
                splitLine = line.split(DELIMITER);

                //Iterates across the array of elements split from the string line
                for (int col = 0; col < splitLine.length; col++) {

                    //In case the element is the last
                    if (col == splitLine.length - 1) {

                        //Select the desired behavior
                        if (splitLine[col].equals("2")) {
                            result[row][col] = 0;
                            result[row][col + 1] = 1;
                        } else if (splitLine[col].equals("4")) {
                            result[row][col] = 1;
                            result[row][col + 1] = 0;
                        }

                    } else {
                        //Otherwise, assign the value divided by 10
                        result[row][col] = Double.parseDouble(splitLine[col]) / 10;
                    }
                }

                //Increases the rowcount
                row++;
            }

            //Closes the resource - You can research about "try with resources for automatic closing"
            br.close();
            fr.close();

            //Test
            for (int i = 0; i < result.length; i++) {
                System.out.print("{");
                for (int j = 0; j < result[i].length; j++) {
                    System.out.print(result[i][j]);
                    if (j != result[i].length - 1) {
                        System.out.print(",");
                    }
                }
                System.out.print("}");
                if (i != result.length - 1) {
                    System.out.print(",");
                }
            }

        } catch (IOException e) {
            //Print error if file not found
            System.out.println("Can't read the file");
        }
    }
 }
相关问题