Re: addition of 2 matrix number
SamuelXiao wrote:
my program is to add 2 matrix number from 2 txt file:
Scanner in1 = new Scanner(System.in);
Scanner in2 = new Scanner(System.in);
// user input file:
String mat1 = in1.next();
String mat2 = in2.next();
but it doesn't work, anyone know how to fix it?
import java.io.Console;
import java.util.ArrayList;
import java.util.List;
public class Scratch {
public static void main(String[] args) {
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
String m1Path = c.readLine("Enter m1 path: ");
int m1Rows =
Integer.parseInt(
c.readLine("Enter m1 number of rows: "));
int m1Cols =
Integer.parseInt(
c.readLine("Enter m1 number of columns: "));
List<String> m1RowStrings =
new ArrayList<String>(m1Rows);
for (int idx = 0; idx < m1Rows; idx++) {
m1RowStrings.add(c.readLine("Enter m1 row# " + idx));
}
String m2Path = c.readLine("Enter m2 path: ");
int m2Rows =
Integer.parseInt(
c.readLine("Enter m2 number of rows: "));
int m2Cols =
Integer.parseInt(
c.readLine("Enter m2 number of columns: "));
List<String> m2RowStrings =
new ArrayList<String>(m2Rows);
for (int idx = 0; idx < m2Rows; idx++) {
m2RowStrings.add(c.readLine("Enter m2 row# " + idx));
}
StringBuilder m1Out = new StringBuilder();
m1Out.append("m1 path = " + m1Path + "\n");
m1Out.append("m1 # of rows = " + m1Rows + "\n");
m1Out.append("m1 # of columns = " + m1Cols + "\n");
for(String row : m1RowStrings) {
m1Out.append(row + "\n");
}
m1Out.append("\n");
StringBuilder m2Out = new StringBuilder();
m2Out.append("m2 path = " + m2Path + "\n");
m2Out.append("m2 # of rows = " + m2Rows + "\n");
m2Out.append("m2 # of columns = " + m2Cols + "\n");
for(String row : m2RowStrings) {
m2Out.append(row + "\n");
}
m2Out.append("\n");
System.err.print(m1Out);
System.err.print(m2Out);
}
}