如何在java中从txt文件中读取两个矩阵

因此,我得到了一个包含两个3x3矩阵的.txt文件,需要携带这些矩阵来加法,乘法,减法并执行标量乘法,其中程序将仅采用一个矩阵,用户将为操作输入一个数字。
问题是让程序只读取矩阵。
以下是我得到的.txt文件,其中前两个数字是大小:


3 3
12 34 45
34 -12 56
76 12 -1
@
3 3
8 13 45
67 0 12
12 -12 3


那里有救命稻草吗?


编辑1

这就是我到目前为止所拥有的,乘法是有效的,我让用户输入矩阵,但现在只是给出一些奇怪的答案,我错过了什么?

import java.io.*;


public class ReadingTest {

    public static void main(String[] args) throws IOException {

        BufferedReader reader;

        reader = new BufferedReader(new FileReader("matrix2.txt"));


        String firstDimension = reader.readLine();

        String[] split = firstDimension.split(" ");

        int firstX = Integer.parseInt(split[0]);

        int firstY = Integer.parseInt(split[0]);


        int[][] first = new int[firstX][firstY];


        for (int i = 0; i < firstX; i++) {

            String[] line;

            line = reader.readLine().split(" ");


            for (int j = 0; j < firstY; j++) {

                first[i][j] = Integer.parseInt(line[j]);

            }


        }


        // Read "@"

        reader.readLine();


        String secondDimension = reader.readLine();

        String[] split2 = secondDimension.split("");

        int secX = Integer.parseInt(split2[0]);

        int secY = Integer.parseInt(split2[0]);


        int[][] second = new int[secX][secY];


        for (int i = 0; i < secX; i++) {

            String[] line;

            line = reader.readLine().split(" ");


            for (int j = 0; j < secY; j++) {

                second[i][j] = Integer.parseInt(line[j]);

            }


        }


        // System.out.println(Arrays.deepToString(second));


        multiply(first, second);


        reader.close();

    }


    public static void multiply(int[][] first, int[][] second) {

        for (int i = 0; i < first.length; i++) {

            int total = 0;

            for (int j = 0; j < second[0].length; j++) {

                int fnum = first[i][j];

                int snum = second[j][i];

                int product = fnum * snum;

                total += product;

            }

            System.out.print(total + " ");

        }

    }

}


PIPIONE
浏览 151回答 1
1回答

慕田峪7331174

您必须:读取第一行拆分它以获得尺寸阅读下一行(关于尺寸)读取特殊字符 (@)重复读取您在那里的第一个数组:static void readFile() throws IOException {&nbsp; &nbsp; BufferedReader reader;&nbsp; &nbsp; reader = new BufferedReader(new FileReader("file.txt"));&nbsp; &nbsp; String firstDimension = reader.readLine();&nbsp; &nbsp; String[] split = firstDimension.split(" ");&nbsp; &nbsp; int firstX = Integer.parseInt(split[0]);&nbsp; &nbsp; int firstY = Integer.parseInt(split[0]);&nbsp; &nbsp; int[][] first = new int[firstX][firstY];&nbsp; &nbsp; for (int i = 0; i < firstX; i++) {&nbsp; &nbsp; &nbsp; &nbsp; String[] line = reader.readLine().split(" ");&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < firstY; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first[i][j] = Integer.parseInt(line[j]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Read "@"&nbsp; &nbsp; reader.readLine();&nbsp; &nbsp; System.out.println(Arrays.deepToString(first));}和基于输入:3 312 34 4534 -12 5676 12 -1@3 38 13 4567 0 1212 -12 3输出应为:[[12, 34, 45], [34, -12, 56], [76, 12, -1]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java