-
-
Save eMahtab/297419cb805fe93346c0 to your computer and use it in GitHub Desktop.
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(); | |
} | |
} | |
} |
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at df.array_sum.main(array_sum.java:26) i am getting this error
how can use this for giving char
thank you
cool
/*
- 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();
}
}
}
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.
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] + " ");
}
}
}
}
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
You need to iterate and keep the index as rows and columns as you are iterating over them, not the input you take from the user (ie. x & y). Those are just the threshold values until when the rows and columns variable would run up to.
//old
array[x][y]=input.nextInt();
//new
array[row][col]=input.nextInt();
//old
System.out.println(array[x][y] + "\t");
//new
System.out.println(array[row][col] + "\t");