2d Array In Java User Input

5 min read Jun 22, 2024
2d Array In Java User Input

2D Array in Java with User Input

A 2D array, also known as a matrix, is a data structure used to store data in a tabular format. In Java, you can create and manipulate 2D arrays, and one common task is to fill them with user input. This article will guide you through creating and filling a 2D array using user input in Java.

Creating a 2D Array

First, you need to declare and initialize a 2D array. The syntax is:

dataType[][] arrayName = new dataType[rows][columns];

Where:

  • dataType is the data type of elements in the array (e.g., int, double, String).
  • arrayName is the name you choose for the array.
  • rows is the number of rows in the array.
  • columns is the number of columns in the array.

For example, to create a 2D array of integers with 3 rows and 4 columns:

int[][] myArray = new int[3][4];

Getting User Input

To get user input, you'll use the Scanner class. Here's a code snippet to get input from the user:

Scanner scanner = new Scanner(System.in);

This creates a Scanner object named scanner that reads input from the console (System.in).

Filling the 2D Array with User Input

Now, you can use a nested loop to iterate through each element of the 2D array and get input from the user for each element:

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < columns; j++) {
        System.out.print("Enter element at [" + i + "][" + j + "]: ");
        myArray[i][j] = scanner.nextInt(); // Reads integer input
    }
}

This code iterates through each row (i) and each column (j) of the array. For each element, it prompts the user to enter a value and stores it in the corresponding array element.

Complete Example

Here's a complete example demonstrating how to create a 2D array, get user input, and fill it:

import java.util.Scanner;

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

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();
        System.out.print("Enter the number of columns: ");
        int columns = scanner.nextInt();

        int[][] myArray = new int[rows][columns];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print("Enter element at [" + i + "][" + j + "]: ");
                myArray[i][j] = scanner.nextInt();
            }
        }

        // Print the array
        System.out.println("The 2D array is:");
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(myArray[i][j] + " ");
            }
            System.out.println();
        }
    }
}

This code first asks the user for the number of rows and columns for the array. Then, it uses nested loops to get user input for each element and store it in the array. Finally, it prints the contents of the array.

Conclusion

This article provided a comprehensive guide on creating a 2D array in Java and filling it with user input. You can apply this knowledge to various programming scenarios involving tabular data representation. Remember to adjust the data type and input prompt based on your specific requirements.

Related Post


Latest Posts