开发者

Multidimension Array In Java [closed]

开发者 https://www.devze.com 2023-04-12 17:47 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

Basically, 开发者_开发技巧user have to specify how the many row and column they want [n*n] and after that user will input value and it will store in multidimension array.I run the code but getting error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

at Total.main(Total.java:30)

Code:

import java.util.*;

public class Total
{
public static void main(String args[])
{
int n,value;
Scanner input = new Scanner(System.in);

System.out.println("Enter integer for row and column:");
n = input.nextInt();

int arrX[][] = new int [n][n];

for(int i=0; i < n; i++ )
{
    for(int j=0; j < n; i++)
    {
        System.out.printf("Enter integer [%d],[%d]:",i,j);
        value = input.nextInt();
        arrX[i][j] = value;
    }
}
  }
}


In the line:

for(int j=0; j < n; i++)

you increase i where it should be j like this:

for(int j=0; j < n; j++)


change this line:

for(int j=0; j < n; **j**++)

it's a typo perhaps


You are incrementing i in both loops.

import java.util.*;

    public class Total
    {
    public static void main(String args[])
    {
    int n,value;
    Scanner input = new Scanner(System.in);

    System.out.println("Enter integer for row and column:");
    n = input.nextInt();

    int arrX[][] = new int [n][n];

    for(int i=0; i < n; i++ )
    {
        for(int j=0; j < n; j++)
        {
            System.out.printf("Enter integer [%d],[%d]:",i,j);
            value = input.nextInt();
            arrX[i][j] = value;
        }
    }
      }
    }


In your second loop you do i++ instead of j++


The problem is you increment the wrong variable in the second for:

for(int j=0; j < n; i++)

Instead of i you must type j

Works fine if you change this.

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号