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();
        }
  }
}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.