Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java将.txt文件读取为二维数组_Java_Arrays_Multithreading_Multidimensional Array - Fatal编程技术网

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

Java将.txt文件读取为二维数组,java,arrays,multithreading,multidimensional-array,Java,Arrays,Multithreading,Multidimensional Array,所以我有一个任务,要求我使用10个线程从matrix.txt文件中读取10列数据。我在将文件转换为2d数组时遇到问题,因为我总是收到NumberFormatException。对于10k元素的10列1000行,文件的样式如下所示。在txt文件中,数字被格式化为列,每个记录之间有不同的空白 9510.0 5880.0 8923.0 21849.0 3295.0 17662.0 23931.0 31401.0 13211.0

所以我有一个任务,要求我使用10个线程从matrix.txt文件中读取10列数据。我在将文件转换为2d数组时遇到问题,因为我总是收到NumberFormatException。对于10k元素的10列1000行,文件的样式如下所示。在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 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();
        }
    }


    }
import java.io.*;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.*;
导入javax.swing.JOptionPane;
导入java.io.*;
导入java.util.*;
公共类主线程{
公共静态int行,cols;
公共静态int[][]单元格;
/**
*main读取文件并启动
*图形显示
*/
公共静态void main(字符串[]args)引发异常{
//在中读取整个文件
列出myFileLines=Files.readAllLines(path.get(“bigMatrix.txt”);
//删除所有空行
对于(int i=myFileLines.size()-1;i>=0;i--){
if(myFileLines.get(i).isEmpty()){
删除(i);
}
}
//使用从文件中读取的行数声明二维数组
double[]intArray=新的双精度[myFileLines.size()][];
//遍历每行以确定列数
对于(int i=0;i

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

正如异常名称所示,例如,
9510.0
不是正确的整数格式,因为
.0
部分表示浮点或双精度。使用
Float.parseFloat()
和一个
Float
数组,或者如果确实需要,可以使用
intArray[i][j]=(int)Float.parseFloat(splitLine[j])将其转换为int

您的代码中(至少)有两个问题可能导致NumberFormatException

首先,您正在执行
split(“\\s”)
,它将在单个空白字符上拆分。这意味着相邻的两个空格将导致一个空字符串“位于”它们之间,并且该空字符串无法解析为数字

第二个是对非整数(它们有小数部分)的数字使用
Integer.parseInt()
。您可能需要
Double.parseDouble()
在这里

最后,在您的示例代码中没有讨论您试图用线程做什么或为什么这样做。如果您有关于线程的问题,请将其作为另一个问题打开,因为它们似乎与此问题无关