patternjavaModerate
Matrix multiplication in Java
Viewed 0 times
matrixjavamultiplication
Problem
I am taking input from a text file, performing matrix multiplication, then outputting to another text file. I just want to be a better programmer, so please be a detailed as you want. Comments, naming, formatting, etc.
```
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class MartixMain {
public static void main(String [] args){
BufferedReader br = null;
BufferedWriter bw = null;
FileWriter fw = null;
List numbers = new ArrayList<>();
// This is the rows and columns for the matries
int row1,column1,row2,column2;
try {
Scanner input = new Scanner(new File("C:/Users/jeremiahlukus /Desktop/New folder/jj.txt"));
// adds numbers from .txt to ArrayList numbers
while(input.hasNextInt()){
numbers.add(input.nextInt());
}
}
catch(FileNotFoundException e){
System.err.println("Unable to find the file: fileName");
}
row1 = numbers.get(0);
column1 = numbers.get(1);
row2 = numbers.get(2);
column2 = numbers.get(3);
if(column1 != row2){
System.out.println("Column 1 is not equal to row 2. Matrix Multiplication is not possible.");
try{
fw = new FileWriter(new File("mytextfile.txt"));
fw.write(String.format("Column 1 is not equal to row 2. Matrix Multiplication is not possible."));
fw.close();
}catch(Exception e){
System.out.println("NONE");
}
}else{
//checking to see if they hold the right numbers
System.out.println("Matrix 1 has " + row1 + " rows and " + column1+ " columns");
System.out.println("Matrix 2 has " + row2 + " rows and " + column2+ " columns");
// This puts the first four numbers as the dimensions of int [][]
int [][] matrix1 = new int [row1][column1];
int [][] matrix2 = new int [row2][column2];
```
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class MartixMain {
public static void main(String [] args){
BufferedReader br = null;
BufferedWriter bw = null;
FileWriter fw = null;
List numbers = new ArrayList<>();
// This is the rows and columns for the matries
int row1,column1,row2,column2;
try {
Scanner input = new Scanner(new File("C:/Users/jeremiahlukus /Desktop/New folder/jj.txt"));
// adds numbers from .txt to ArrayList numbers
while(input.hasNextInt()){
numbers.add(input.nextInt());
}
}
catch(FileNotFoundException e){
System.err.println("Unable to find the file: fileName");
}
row1 = numbers.get(0);
column1 = numbers.get(1);
row2 = numbers.get(2);
column2 = numbers.get(3);
if(column1 != row2){
System.out.println("Column 1 is not equal to row 2. Matrix Multiplication is not possible.");
try{
fw = new FileWriter(new File("mytextfile.txt"));
fw.write(String.format("Column 1 is not equal to row 2. Matrix Multiplication is not possible."));
fw.close();
}catch(Exception e){
System.out.println("NONE");
}
}else{
//checking to see if they hold the right numbers
System.out.println("Matrix 1 has " + row1 + " rows and " + column1+ " columns");
System.out.println("Matrix 2 has " + row2 + " rows and " + column2+ " columns");
// This puts the first four numbers as the dimensions of int [][]
int [][] matrix1 = new int [row1][column1];
int [][] matrix2 = new int [row2][column2];
Solution
In addition to what Hosch has told you:
Make use of try with resources
You utilize several classes which implement the
As an example, rather than the following:
You can write:
The newline character
Everywhere you're calling
e.g. This chunk:
Could be just:
Be DRY
Anytime you see yourself encountering repition like the following:
it's a big indication your code could probably do with some refactoring. You could use simple loop:
or better yet structured in a method
which you could invoke, simply, by calling
Don't just catch Exception
While many of us are guilty of just wanting the initial version before changing it, you never want to only catch Exception, for obvious reasons. Be as specific as possible.
Declare variables on separate lines
Let's examine this line:
Firstly, for the sake of readability you should get in the habit of adding a space after your commas, but even still conventionally each variable should have its on own separate line. It may even seem 'wasteful' to you to do it that way, but the distinct locations make things more readable, flexible and maintainable.
Make use of try with resources
You utilize several classes which implement the
Closable interface, but aren't explicitly freeing resources you use. Employing a try-with-resources statement will amend that with the benefit of reducing the scope of some variables you currently declare outside.As an example, rather than the following:
try {
Scanner input = new Scanner(new File("C:/Users/jeremiahlukus /Desktop/New folder/jj.txt"));
// adds numbers from .txt to ArrayList numbers
while(input.hasNextInt()){
numbers.add(input.nextInt());
}
}You can write:
try (Scanner input = new Scanner(
new File("C:/Users/jeremiahlukus/Desktop/New folder/jj.txt") {
// logic
}The newline character
Everywhere you're calling
System.lineSeparator() could be replaced with a '\n' newline character preceding where you'd want it.e.g. This chunk:
fw.write(String.format(Arrays.deepToString(matrix2)));
fw.write(System.lineSeparator()); //new line
fw.write(String.format("Product of Matrix 1 and Matrix 2 is: "));
fw.write(System.lineSeparator()); //new lineCould be just:
fw.write(String.format(Arrays.deepToString(matrix2)));
fw.write(String.format("%nProduct of Matrix 1 and Matrix 2 is:%n"));Be DRY
Anytime you see yourself encountering repition like the following:
numbers.remove(0);
numbers.remove(0);
numbers.remove(0);
numbers.remove(0);it's a big indication your code could probably do with some refactoring. You could use simple loop:
for (int i = 1; i <= 4; i++) {
numbers.remove(0);
}or better yet structured in a method
private static void remove(List target, iterations) {
for (int i = 1; i <= iterations; i++) {
target.remove(0);
}
}which you could invoke, simply, by calling
remove(numbers, 4); of course now you have the added benefit of recycling the method elsewhere if necessary.Don't just catch Exception
While many of us are guilty of just wanting the initial version before changing it, you never want to only catch Exception, for obvious reasons. Be as specific as possible.
Declare variables on separate lines
Let's examine this line:
int row1,column1,row2,column2; Firstly, for the sake of readability you should get in the habit of adding a space after your commas, but even still conventionally each variable should have its on own separate line. It may even seem 'wasteful' to you to do it that way, but the distinct locations make things more readable, flexible and maintainable.
Code Snippets
try {
Scanner input = new Scanner(new File("C:/Users/jeremiahlukus /Desktop/New folder/jj.txt"));
// adds numbers from .txt to ArrayList numbers
while(input.hasNextInt()){
numbers.add(input.nextInt());
}
}try (Scanner input = new Scanner(
new File("C:/Users/jeremiahlukus/Desktop/New folder/jj.txt") {
// logic
}fw.write(String.format(Arrays.deepToString(matrix2)));
fw.write(System.lineSeparator()); //new line
fw.write(String.format("Product of Matrix 1 and Matrix 2 is: "));
fw.write(System.lineSeparator()); //new linefw.write(String.format(Arrays.deepToString(matrix2)));
fw.write(String.format("%nProduct of Matrix 1 and Matrix 2 is:%n"));numbers.remove(0);
numbers.remove(0);
numbers.remove(0);
numbers.remove(0);Context
StackExchange Code Review Q#118416, answer score: 11
Revisions (0)
No revisions yet.