Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public static int[][] enter2DPosArray() : The method enters a two-dimensional in

ID: 3689647 • Letter: P

Question

public static int[][] enter2DPosArray(): The method enters a two-dimensional integer array from the user. It forces the user to enter positive (> 0) values for the number of rows and columns. It raises an InputMismatchException if the user enters anything other than positive (> 0) values for any array entry. The function will need to declare and use a Scanner object. The Scanner object should be closed before any exceptions are raised and before the method returns if no exceptions are raised.

The following shows a sample run in which the user types a non-positive value when prompted for the number of rows:

How many rows (> 0) should the array have? -1
How many rows (> 0) should the array have? 1
How many columns (> 0) should the array have? 5
Enter a positive (> 0) integer value: 1
Enter a positive (> 0) integer value: 2
Enter a positive (> 0) integer value: 3
Enter a positive (> 0) integer value: 4
Enter a positive (> 0) integer value: 5
The original array values:
1 2 3 4 5
The transposed array values:
1
2
3
4
5
The 1D array:
1 2 3 4 5

Explanation / Answer

Solution:

package com.nancy.chegg.qanda;

import java.util.InputMismatchException;
import java.util.Scanner;

public class ArrayClass {

   public static void main(String[] args) {
       enter2DPosArray();
   }

   public static int[][] enter2DPosArray() {
       int row;
       int col;
       int arr[][] = null;

       Scanner scan = new Scanner(System.in);
       try {
           do {
               System.out
                       .println("How many rows (> 0) should the array have? ");
               row = scan.nextInt();
           } while (row <= 0);
           do {
               System.out.println(
                       "How many columns (> 0) should the array have?");
               col = scan.nextInt();
           } while (col <= 0);
           arr = new int[row][col];
           for (int i = 0; i < row; i++) {
               for (int j = 0; j < col; j++) {

                   System.out
                           .println("Enter a positive (> 0) integer value: ");
                   arr[i][j] = scan.nextInt();
                   if (arr[i][j] < 0)
                       throw new InputMismatchException();
               }
           }

           System.out.println("The original array values : ");
           for (int i = 0; i < row; i++) {
               System.out.println(" ");
               for (int j = 0; j < col; j++) {
                   System.out.print(arr[i][j] + " ");
               }
           }

           System.out.println(" The transposed array values: : ");
           for (int i = 0; i < col; i++) {
               System.out.println(" ");
               for (int j = 0; j < row; j++) {
                   System.out.print(arr[j][i] + " ");
               }
           }

           if (row == 1 || col == 1) {
               System.out.println(" The 1D array : ");
               for (int i = 0; i < row; i++) {
                   for (int j = 0; j < col; j++) {
                       System.out.print(arr[i][j] + " ");
                   }
               }
           }

       } catch (InputMismatchException e) {
           System.out.println(
                   "InputMismatchException : Yous have enetered value less than 0.");
           System.out.println("Exit!");
       } finally {
           scan.close();
       }
       return arr;

   }
}

Sample run :

How many rows (> 0) should the array have? 1
How many columns (> 0) should the array have?3
Enter a positive (> 0) integer value: 1
Enter a positive (> 0) integer value: 2
Enter a positive (> 0) integer value: 3
The original array values :

1 2 3
The transposed array values: :

1

2

3

The 1D array :
1 2 3

____________________________________________

How many rows (> 0) should the array have?
-1
How many rows (> 0) should the array have?
1
How many columns (> 0) should the array have?
4
Enter a positive (> 0) integer value:
-1
InputMismatchException : Yous have enetered value less than 0.
Exit!

_____________________________________________________________

How many rows (> 0) should the array have?
2
How many columns (> 0) should the array have?
3
Enter a positive (> 0) integer value:
1
Enter a positive (> 0) integer value:
2
Enter a positive (> 0) integer value:
3
Enter a positive (> 0) integer value:
4
Enter a positive (> 0) integer value:
5
Enter a positive (> 0) integer value:
6
The original array values :


1 2 3

4 5 6
The transposed array values: :


1 4

2 5

3 6