Skip to content

Instantly share code, notes, and snippets.

@eMahtab
Created November 19, 2015 13:12
Show Gist options
  • Save eMahtab/297419cb805fe93346c0 to your computer and use it in GitHub Desktop.
Save eMahtab/297419cb805fe93346c0 to your computer and use it in GitHub Desktop.
Java program to take 2D array as input from user.
import java.util.Scanner;
public class TwoDArrayInput{
public static void main(String args[]){
System.out.print("Enter 2D array size : ");
Scanner sc=new Scanner(System.in);
int rows=sc.nextInt();
int columns=sc.nextInt();
System.out.println("Enter array elements : ");
int twoD[][]=new int[rows][columns];
for(int i=0; i<rows;i++)
{
for(int j=0; j<columns;j++)
{
twoD[i][j]=sc.nextInt();
}
}
System.out.print("\nData you entered : \n");
for(int []x:twoD){
for(int y:x){
System.out.print(y+" ");
}
System.out.println();
}
}
}
@Rahulkharvi10
Copy link

thank you

@Jondi-Ts
Copy link

cool

@Ahmed-Ayman25
Copy link

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package com.mycompany.mavenproject3;

import java.util.Scanner;
import java.util.Arrays;

/**
*

  • @author mimoe
    */
    public class Exercise {

System.out.println(" Enter number of Rows");
int row=s.nextInt();
System.out.println(" Enter number of col");
int col=s.nextInt();
int arr[][]=new int [row][col];
System.out.println(" Enter Elements of Array");
for ( row = 0; row < arr.length; row++) {
for ( col = 0; col < arr[row].length; col++) {

         arr[row][col]=s.nextInt();
     }
 }
 for ( row = 0; row < arr.length; row++) {
     for (col = 0; col < arr[row].length; col++) {
         System.out.print(arr[row][col]+" ");
         
     }
     System.out.println();
 }

}
}

@farhaneas11
Copy link

import java.io.*;
import java.util.Arrays;
//package assignments.ass23;
import java.util.Scanner;

/**

  • 2darray
    */
    public class darray {
    public static void getArray(int n,int[][] array) {
    Scanner sc = new Scanner(System.in);
    array = new int[n][n];
    System.out.println("Enter the elements :");
    for(int i= 0;i<n;i++){
    for(int j = 0;j<n;j++){
    array[i][j] = sc.nextInt();
    }

     }
    

    }
    public static void displayArray(int n,int[][] array) {

     System.out.println("the elements are :");
     for(int i= 0;i<n;i++){
         for(int j = 0;j<n;j++){
             System.out.println("\t"+array[i][j]);
         }
         System.out.println("\n");
     }
    

    }
    public static void main(String[] args) {
    int n;
    int[][] array;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the limit");
    n = sc.nextInt();
    array = new int[n][n];
    getArray(n, array);
    displayArray(n, array);
    }
    }

please let me know where I went wrong.

@Manmeet121999
Copy link

import java.util.Scanner;
public class Multidimensional_Arrays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int flates[][] = new int[2][3];
System.out.println("Enter array elements = ");
for (int i = 0; i < flates.length; i++) {
for (int j = 0; j <= flates.length; j++) {
flates[i][j] = sc.nextInt();
System.out.print(flates[i][j] + " ");
}
}
System.out.println();
//reverse
System.out.println("Reverse of Multi-dimensional Array");
for (int i = flates.length - 1; i >= 0; i--) {
for (int j = flates.length; j >= 0; j--) {
System.out.print(flates[i][j] + " ");
}
}
}
}

@Anand4510
Copy link

Bro your "x" and "y" are the size of row and column, so in the line
// Let's consider your row =2, and column=2
array[x][y] = input.nextInt();
array[2][2] = 20 // let's consider ur 1st input is 20.
You are directly giving input to, the 2nd row 2nd column element.
So for the first input i.e., array[2][2] will take input as 20. In the next iteration, again you are giving the input for the same array[2][2]th element.
There you will get the ArrayIndexOutOfRange
After

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment