In: Computer Science
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist.
first input is the name of the first file and it has
2 (length)
try catch method
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MatrixMultiplication {
//private static final String FILE1 = "matrix1.txt";
//private static final String FILE2 = "matrix2.txt";
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Scanner fileReader;
int rows1 = 0, rows2 = 0, cols1 = 0, cols2 = 0;
// read file1
System.out.print("Please enter the name of the first file:
");
String fileName1 = sc.nextLine().trim();
try
{
fileReader = new Scanner(new File(fileName1));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine();
cols1 = line.split(" ").length;
rows1++;
}
if(rows1 < 2)
{
System.out.println(fileName1 + " does not have 2 lines!");
System.exit(0);
}
}catch(FileNotFoundException fnfe){
System.out.println(fileName1 + " could not be found!");
System.exit(0);
}
// read file2
System.out.print("Please enter the name of the second file:
");
String fileName2 = sc.nextLine().trim();
try
{
fileReader = new Scanner(new File(fileName2));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine();
cols2 = line.split(" ").length;
rows2++;
}
if(rows2 < 2)
{
System.out.println(fileName2 + " does not have 2 lines!");
System.exit(0);
}
}catch(FileNotFoundException fnfe){
System.out.println(fileName2 + " could not be found!");
System.exit(0);
}
int[][] arr1 = new int[rows1][cols1];
int[][] arr2 = new int[rows2][cols2];
// read file1 for matrix
try
{
fileReader = new Scanner(new File(fileName1));
while(fileReader.hasNextLine())
{
for(int i = 0; i < arr1.length; i++)
{
String line[] = fileReader.nextLine().split(" ");
for(int j = 0; j < line.length; j++)
{
arr1[i][j] = Integer.parseInt(line[j]);
}
}
}
}catch(FileNotFoundException fnfe){
System.out.println(fileName1 + " could not be found!");
System.exit(0);
}
// read file2 for matrix
try
{
fileReader = new Scanner(new File(fileName2));
while(fileReader.hasNextLine())
{
for(int i = 0; i < arr2.length; i++)
{
String line[] = fileReader.nextLine().split(" ");
for(int j = 0; j < line.length; j++)
{
arr2[i][j] = Integer.parseInt(line[j]);
}
}
}
}catch(FileNotFoundException fnfe){
System.out.println(fileName1 + " could not be found!");
System.exit(0);
}
int[][] product = matrixProduct(arr1, arr2, rows1, cols1,
cols2);
System.out.println("The product matrix is:");
printMatrix(product);
}
private static int[][] matrixProduct(int[][] arr1, int[][] arr2,
int rows1, int cols1, int cols2)
{
int[][] product = new int[rows1][cols2];
for(int i = 0; i < rows1; i++)
{
for(int j = 0; j < cols2; j++)
{
for(int k = 0; k < cols1; k++)
{
product[i][j] = arr1[i][k] * arr2[k][j];
}
}
}
return product;
}
private static void printMatrix(int[][] arr)
{
for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[0].length; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
****************************************************************** SCREENSHOT *********************************************************