为java运行预定义的输入脚本

为java运行预定义的输入脚本,java,Java,以下代码编译并正确运行。我想运行一组预定义的输入,一个名为commands的脚本。当我执行javalab1

以下代码编译并正确运行。我想运行一组预定义的输入,一个名为commands的脚本。当我执行
javalab1
时,它会给出以下错误

Exception in thread "main" java.lang.NullPointerException
        at java.io.File.<init>(File.java:222)
        at Lab1.MatrixReader(Lab1.java:207)
        at Lab1.main(Lab1.java:120)
代码如下:

import java.io.*;
import java.util.*;



class Matrix { 

double[][] element;
int rows, cols;

Matrix(){

}
Matrix(int rows, int cols) {
    this.rows = rows;
    this.cols = cols;
    element = new double[rows][cols];
}

public double getValue(int row, int col) {
    return element[row][col];
}

public void setValue(int row, int col, double value) {
    element[row][col] = value;
}

public int getNoRows() {  // returns the total number of rows
    return rows;
}

public int getNoCols() { // returns the total number of cols
    return cols;
}

// The methods for the main calculations
public Matrix AddMatrix(Matrix m2) {
    int row1 = getNoRows();
    int col1 = getNoCols();
    Matrix result = new Matrix(row1, col1);

    for (int i = 0; i < row1; i++) {
        for (int j = 0; j < col1; j++) {
            result.setValue(i, j, this.getValue(i, j) + m2.getValue(i, j));
        }
    }
    return result;
}

public Matrix MultiplyMatrix(Matrix m2) {
    if (this.getNoCols() != m2.getNoRows()) {
        throw new IllegalArgumentException("matrices can't be multiplied");
    }
    int row2 = this.getNoRows();
    int col2 = m2.getNoCols();
    Matrix result = new Matrix(row2, col2);
    for (int i = 0; i < row2; i++) {
        for (int j = 0; j < col2; j++) {
            result.setValue(i, j, result.getValue(i, j) + this.getValue(i, j) * m2.getValue(i, j));
        }
    }
    return result;

}

public Matrix TransposeMatrix() {
    int row3 = this.getNoCols();
    int col3 = this.getNoRows();
    Matrix result = new Matrix(row3, col3);
    for (int i = 0; i < row3; i++) {
        for (int j = 0; j < col3; j++) {
            result.setValue(i, j, this.getValue(j, i));
        }
    }
    return result;

}

public void DisplayMatrix() {
    for (int i = 0; i < this.getNoRows(); i++) {
        for (int j = 0; j < this.getNoCols();
                j++) {
            System.out.print((this.getValue(i, j)) + " ");
        }
        System.out.print("\n");
    }
    System.out.print("\n");
   }
}



public class Lab1 {

public static void main(String args[]) throws FileNotFoundException {

    int choice;
    Scanner in = new Scanner(System.in);
    Matrix m1 = new Matrix();
    Matrix m2 = new Matrix();
    Matrix m3 = new Matrix(); 
    Boolean loopcont= true;
    while (loopcont){   
    System.out.println("1. Add two matrices \n");
    System.out.println("2. Multiply two matrices \n");
    System.out.println("3. Take transpose of a matrix \n");
    System.out.println("4. Display a matrix \n");
    System.out.println("5. Exit \n");

    System.out.println("Enter your choice \n");
    choice = in.nextInt();
     //Matrix m1;
//Matrix m2;
//Matrix m3;

switch (choice) {
case 1: {
    System.out.println("For the first matrix");
    m1 = MatrixReader();
    m1.DisplayMatrix();
    System.out.println("For the second matrix");
    m2 = MatrixReader();
    m2.DisplayMatrix();
    m3 = new Matrix(m1.getNoRows(), m1.getNoCols());
    m3 = m1.AddMatrix(m2);
    m3.DisplayMatrix();
    break;
}
case 2: {
    System.out.println("For the first matrix");
    m1 = MatrixReader();
    m1.DisplayMatrix();
    System.out.println("For the second matrix");
    m2 = MatrixReader();
    m2.DisplayMatrix();
    m3 = new Matrix(m1.getNoRows(), m1.getNoCols());
    m3 = m1.MultiplyMatrix(m2);
    m3.DisplayMatrix();
    break;

}
case 3: {
    System.out.println("For the first matrix");
    m1 = MatrixReader();
    m1.DisplayMatrix();
    m3 = m1.TransposeMatrix();
    m3.DisplayMatrix();
    break;
}
case 4: {
    int printInput;
    System.out.println("What matrix do you want to print?");
    printInput = in.nextInt();
    switch (printInput) {
    case 1: {
        System.out.println("Printing Matrix m1");
  //      m1 = MatrixReader();
        m1.DisplayMatrix();
        break;
    }
    case 2: {
        System.out.println("Printing Matrix m2");
    //    m2 = MatrixReader();
        m2.DisplayMatrix();
        break;
    }
    case 3: {
        System.out.println("Printing Matrix m3");
      //  m1= MatrixReader();
        //m3 = new Matrix(m1.getNoCols(), m1.getNoRows());
        m3.DisplayMatrix();
        break;
    }
    default: {
        System.out.println("Invalid Input. please enter again");
        break;
    }
    }
    break;
}
case 5: {
    loopcont = false;
    break;
}
default: {
    System.out.println("Incorrect input. Kindly enter again \n");
    break;
  }
}

  }
 }

public static Matrix MatrixReader() throws FileNotFoundException {
    System.out.println("Give the filename for the matrix");
    BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

    String filename = null;
    try{ 
        filename = br.readLine();
   } catch (IOException ioe) {
    System.out.println("IO error");
    System.exit(1);
    }
    //Scanner filename = new Scanner(System.in);
    Scanner scanner = new Scanner(new File(filename));

    scanner.nextLine(); // removes the first line in the input file
    String rowLine = scanner.nextLine();
    String[] arr = rowLine.split("=");
    int rows = Integer.parseInt(arr[1].trim());

    String colLine = scanner.nextLine();
    String[] arr2 = colLine.split("=");
    int cols = Integer.parseInt(arr2[1].trim());
    Matrix test = new Matrix(rows, cols);

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            test.setValue(i, j, scanner.nextDouble());
        }
    }


    return test;

    }
  }
import java.io.*;
导入java.util.*;
类矩阵{
双[]元素;
int行,cols;
矩阵(){
}
矩阵(整数行,整数列){
this.rows=行;
this.cols=cols;
元素=新的双[行][cols];
}
公共双getValue(整数行,整数列){
返回元素[行][col];
}
公共void设置值(整行、整列、双值){
元素[行][列]=值;
}
public int getNoRows(){//返回总行数
返回行;
}
public int getNoCols(){//返回列的总数
返回cols;
}
//主要计算方法
公共矩阵AddMatrix(矩阵m2){
int row1=getNoRows();
int col1=getNoCols();
矩阵结果=新矩阵(第1行,第1列);
对于(int i=0;i
示例文本文件如下所示。我想获取行和列,以便动态声明矩阵,然后存储其值。我得到的错误是INPUTMISMATCH exception。我们将不胜感激

<matrix>
    rows = 2
    cols = 2

1 2
2 4 
</matrix>

行数=2
cols=2
1 2
2 4 
例外
<matrix>
    rows = 2
    cols = 2

1 2
2 4 
</matrix>
Scanner scanner = new Scanner(new File(filename));