Java将.txt文件读取到二维数组

时间:2016-03-04 13:41:42

标签: java arrays multithreading multidimensional-array

所以我的作业要求我使用10个线程从矩阵.txt文件中读取10列数据。我无法将文件转换为二维数组,因为我总是得到一个NumberFormatException。对于10列和1000行10k元素,文件的样式如下所示。在txt文件本身中,数字的格式为每列记录之间有不同空格的列。

9510.0      5880.0      8923.0      21849.0     3295.0      17662.0     23931.0     31401.0     13211.0     18942.0     
11034.0     9002.0      4162.0      4821.0      4217.0      7849.0      22837.0     19178.0     24492.0     14838.0     
7895.0      7337.0      18115.0     8949.0      28492.0     22067.0     12714.0     21234.0     26497.0     18003.0     
846.0       29493.0     21868.0     26037.0     27137.0     28630.0     20373.0     8274.0      21280.0     11475.0     
26069.0     21295.0     16883.0     4448.0      20317.0     21063.0     3540.0      23934.0     14843.0     2757.0      
19348.0     32207.0     7833.0      5495.0      26138.0     20905.0     16135.0     19840.0     10829.0     5993.0      
12538.0     16205.0     18997.0     29450.0     6740.0      970.0       7004.0      17142.0     677.0       23509.0     
5243.0      14107.0     24050.0     8179.0      20050.0     24130.0     13494.0     22593.0     3032.0      7580.0      

以下是我一直在测试扫描仪实现的代码。

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

import javax.swing.JOptionPane;

import java.io.*;
import java.util.*;

public class MainThread {

    public static int rows, cols;
    public static int[][] cells;
    /**
     * main reads the file and starts
     * the graphical display
     */
    public static void main(String[] args) throws Exception {

         // Read the entire file in
        List<String> myFileLines = Files.readAllLines(Paths.get("bigMatrix.txt"));

        // Remove any blank lines
        for (int i = myFileLines.size() - 1; i >= 0; i--) {
            if (myFileLines.get(i).isEmpty()) {
                myFileLines.remove(i);
            }
        }

        // Declare you 2d array with the amount of lines that were read from the file
        double[][] intArray = new double[myFileLines.size()][];

        // Iterate through each row to determine the number of columns
        for (int i = 0; i < myFileLines.size(); i++) {
            // Split the line by spaces
            String[] splitLine = myFileLines.get(i).split("\\s");

            // Declare the number of columns in the row from the split
            intArray[i] = new double[splitLine.length]; 
            for (int j = 0; j < splitLine.length; j++) {
                // Convert each String element to an integer
                intArray[i][j] = Integer.parseInt(splitLine[j]);
            }
        }

        // Print the integer array
        for (double[] row : intArray) {
            for (double col : row) {
                System.out.printf("%5d ", col);
            }
            System.out.println();
        }
    }


    }

任何帮助都会很棒,我也知道一些关于线程和实现Runnable接口的信息,但是关于如何计算每列的最大值以及平均值的任何想法都很棒。

2 个答案:

答案 0 :(得分:0)

正如异常的名称所示,9510.0例如由于.0部分暗示浮点数或双精度而不是正确的整数格式。使用Float.parseFloat()float数组,或者如果您真的需要,使用intArray[i][j] = (int) Float.parseFloat(splitLine[j]);

转换为int

答案 1 :(得分:0)

您的代码存在(至少)两个问题,可能导致NumberFormatException。

首先,你正在做split("\\s"),它将分裂在一个空格字符上。这意味着彼此相邻的两个空格将导致空字符串在&#34;之间#34;它们和这个空字符串不能解析为数字。

第二个是你在非整数的数字上使用Integer.parseInt()(它们有一个小数部分)。你可能想要Double.parseDouble()

最后,您的示例代码中没有任何内容可以讨论您尝试使用线程或为什么要执行的操作。如果您对线程有疑问,请将它们打开为另一个问题,因为它们似乎与此无关。