0

the following code basically creates a matrix using user input such as number of rows and column and then ask for data which will populate the 2d array. i'm struggling to find a way to carry out junit test on these method.

any help would be greatly appreciated.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

        System.out.println("Enter Rows");

        int Rows = scan.nextInt();

        System.out.println("Enter Columns");

        int Columns = scan.nextInt();

        //defining 2D array to hold matrix data
        int[][] matrix = new int[Row][Columns];
        // Enter Matrix Data
        creatematrix(scan, matrix, Rows, Columns);

        // Print Matrix Data
        printMatrix(matrix, Rows, matrixCol);
  }

this method basically takes the user input and creates a matrix based on the specified number of rows and columns

public static void creatematrix(Scanner scan, int[][] matrix, int Rows, int Columns){
     System.out.println("Enter Matrix Data");

          for (int i = 0; i < matrixRow; i++)
          {
              for (int j = 0; j < Columns; j++)
              {
                  matrix[i][j] = scan.nextInt();
              }
          }
  }

the last method prints the matrix onto the console

public static void printMatrix(int[][] matrix, int Rows, int Columns){
    System.out.println("Your Matrix is : ");

        for (int i = 0; i < Rows; i++)
        {
            for (int j = 0; j < matrixCol; j++)
            {
                System.out.print(matrix[i][j]+"\t");
            }

            System.out.println();
        }
  }
}
Emotional_Goose
  • 167
  • 1
  • 13

1 Answers1

1

Possibly a duplicate of JUnit test for console input and output

Basically you can provide your own StringReader based Scanner to createMatrix and printMatrix methods.

NumeroUno
  • 1,100
  • 2
  • 14
  • 34