I want to multiply two matrices. I have 2 files with 2 different integers (matrices).
file1.txt
4 3 4 6
-1 10 4 -1
4 7 2 -8
file2.txt
3 0 0
0 3 0
0 0 3
0 2 4
How can I read these files separately into a two-dimensional array, so that it is convenient to multiply. I've a code where the size of the matrix is indicated at the beginning, but what if there could be different size of matrices? Here is my code with given size:
public static void main(String[] args) throws IOException {
try {
Scanner input = new Scanner(new File(file1.txt));
int m = 3; // I need the size for random matrix
int n = 5; // I need the size for random matrix
int[][] a = new int[m][n];
while (input.hasNextLine()) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
try{
a[i][j] = input.nextInt();
System.out.println("number is "+ a[i][j]);
}
catch (java.util.NoSuchElementException e) {
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
It would be better to implement a separate method reading a file into matrix if the dimensions of the matrix are known:
public static int[][] readFileWithMatrix(String filename, int rows, int cols) throws Exception {
int[][] arr = new int[rows][cols];
try (Scanner input = new Scanner(new File(filename))) { // use try-with-resources to close Scanner
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = input.nextInt();
}
}
}
return arr;
}
Then it would be simpler to use this method:
int[][] arr3x4 = readFileWithMatrix("file1.txt", 3, 4);
int[][] arr4x3 = readFileWithMatrix("file2.txt", 4, 3);
//... do matrix multiplication
Here try the following code which takes all the input from file assuming that it contains only the matrix elements and store it into a dynamic 2D array a
eg, list of list. First I read the first line of the file and take out all the numbers from it which gives me the total number of columns of the matrix. Next I keep on reading the input lines until the end of file which expands the row of the matrix a
public static void main(String[] args) throws IOException {
try {
Scanner input = new Scanner(new File(file1.txt));
List<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
int row=0;
String cols[] = input.nextLine().split(" ");
a.add(row, new ArrayList<Integer>());
for (int j = 0; j < cols.length; j++) {
try {
a.get(row).add(Integer.parseInt(cols[j]));
} catch (Exception e) {
}
}
row++;
while (input.hasNextLine()) {
a.add(row, new ArrayList<Integer>());
for (int j = 0; j < cols.length; j++) {
try {
a.get(row).add(input.nextInt());
} catch (java.util.NoSuchElementException e) {
}
}
row++;
}
System.out.println("Rows: "+row+" Columns: "+cols.length);
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a.get(i).size(); j++) {
System.out.println("Number is "+ a.get(i).get(j));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Demo code in here: https://ideone.com/ZEcgPR#stdin
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.