Click here to Skip to main content
15,892,805 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The problem is in output showing 1 error that I donot understand ..
Where is error?

What I have tried:

import java.util.*;


 /*Please dont change class name, Dcoder 
 and class must not be public*/

 //Compiler version JDK 1.8
//output of the given matrix is  // 0 1 2 3 4
                                // 5 6 7 8 9
                                  10 11 12 13 14
                                 
 15 16 17 18 19
       
 class Dcoder
 { 
public static void main(String args[])
 { 

int I,j,k=0;

int A[][]=new int[4][];
for(I=0;I<4;I++){
for(j=0;j<5;j++){

A[I][j]= k;

k++;

}}

for(I=0;I<4;I++){
for(j=0;j<5;j++){
System.out.println("" + A[I][j]);

System.out.println(); 
}}
}
}
Posted
Updated 23-Apr-18 20:01pm

1 solution

Look at your code:
Java
int A[][]=new int[4][];
for(I=0;I<4;I++){
for(j=0;j<5;j++){

A[I][j]= k;

k++;

}}
And let's start by making it a little more readable, shall we?
Java
int a[][] = new int[4][];
for(i = 0; i < 4; i++){
    for(j = 0; j < 5; j++){
        a[i][j] = k;
        k++;
    }
}
Be consistent in your variable naming: local variables should be lowercase, not a mixture, and get your indentation correct! It makes your code a whole load easier to read...
You declare the 2D array, but you only specify one size: so the system doesn't allocate any memory for the "second dimension". No memory, and it will fail when you try to access it.
Try this:
int a[][] = new int[4][5];
It should get you a little further.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900