如何从Java中的txt文件读取两个矩阵 [英] How to read two matrices from a txt file in java

查看:54
本文介绍了如何从Java中的txt文件读取两个矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我得到了一个.txt文件,其中包含两个3x3矩阵,并且需要携带这些矩阵来进行加,乘,减和标量乘法运算,其中程序将仅使用一个矩阵,而用户将输入该操作的编号.
问题是使程序仅读取矩阵.
这是我得到的.txt文件,其中前两个数字是大小:

So, I've been given a .txt file that contains two 3x3 matrices and need to carry those matrices to add, multiply, subtract and do a scalar multiplication, where the program would take only one matrix and the user would input a number for the operation.
The problem is getting the program to read only the matrices.
Here is the .txt file I've been given, where the first two numbers are the size:

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

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

外面有救生员吗?

EDIT1

EDIT1

到目前为止,这是我所拥有的,乘法方法在我让用户输入矩阵时起作用,但是现在只是给出一些奇怪的答案,我想念的是什么?

This is what i have so far, the multiply method was working whe I was letting the user input the matrix but now is just giving some odd answer, what am I missing?

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 + " ");
        }
    }
}

推荐答案

您必须:

  • 阅读第一行

  • read first line

将其拆分以获取尺寸

阅读下一行(关于尺寸)

read next lines (regards dimensions)

读取特殊字符( @ )

重复

读取那里的第一个数组:

Reading first array you have there:

static void readFile() throws IOException {
    BufferedReader reader;
    reader = new BufferedReader(new FileReader("file.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 = reader.readLine().split(" ");

        for (int j = 0; j < firstY; j++) {
            first[i][j] = Integer.parseInt(line[j]);
        }

    }

    // Read "@"
    reader.readLine();

    System.out.println(Arrays.deepToString(first));


}

,并根据输入:

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

输出应为:

[[12, 34, 45], [34, -12, 56], [76, 12, -1]]

这篇关于如何从Java中的txt文件读取两个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆