0
        String[][] tArray=new String[2][4];

    for (int row=0; row<=2;row++){

        for (int col=0,count=0;col<=4;col++ ,count++){
            Scanner input=new Scanner(System.in);
            System.out.print("Please Enter the name " +count+ ":");
            tArray[row][col]=input.next();
        }

    }

    for (int row=0; row<=2;row++){

        for (int col=0;col<=4;col++){
            System.out.println(tArray[row][col]);
        }

    }

Error : -

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at array.testArray.main(testArray.java:15)

3 Answers 3

2

The problem is

        for (int col=0,count=0;col<=4;col++ ,count++){

you can only go upto 3 and not 4, as indices start from 0. Use this

        for (int col=0,count=0;col<4;col++ ,count++){

Same here

for (int row=0; row<=2;row++){   // incorrect

do this

for (int row=0; row<2;row++){
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! added this to the answer.
2

Remember that Java's indexes start from zero and end at length - 1! You're trying to find three "rows" of the array where there are only two:

for (int row=0; row<=2;row++){ //0, 1, 2 - that's three options even though the array's size is only 2

and likewise five columns where there are only four:

for (int col=0,count=0;col<=4;col++ ,count++){ //0, 1, 2, 3, 4 - that makes five

Using < in place of <= should fix this particular issue, although I would also recommend using the length attribute of the array to find out the actual size of the array each time instead of hard-coding it. It's safer from human error and works even if you change the size of your array:

for (int row=0; row < tArray.length; row++){

and

for (int col=0,count=0;col < tArray[row].length; col++ ,count++){

Finally, even though this is a bit off-topic, col and count seem to always have the same value so I'd suggest removing either.

Comments

0

Reason: You are iterating the Loop more than the Items Length.

problem is here:

String[][] tArray=new String[2][4];//means 2 rows and 4 columns

for (int row=0; row<=2;row++)//row 0 to 2 => 0,1,2 iterates 3 times
{

    for (int col=0,count=0;col<=4;col++ ,count++)//column 0 to 4 => 0,1,2,3,4 iterates 5 times
    {
    //code   
    }

}

for (int row=0; row<=2;row++)//row 0 to 2 => 0,1,2 iterates 3 times
{

    for (int col=0;col<=4;col++)//column 0 to 4 => iterates 0,1,2,3,4
    {
   //code
    }

}

Solution:

 String[][] tArray=new String[2][4];

    for (int row=0; row<2;row++){

        for (int col=0,count=0;col<4;col++ ,count++){
            Scanner input=new Scanner(System.in);
            System.out.print("Please Enter the name " +count+ ":");
            tArray[row][col]=input.next();
        }

    }

    for (int row=0; row<2;row++){

        for (int col=0;col<4;col++){
            System.out.println(tArray[row][col]);
        }

    }

3 Comments

Thank you very much. I did the changes. but it displays the same error, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at array.testArray.main(testArray.java:23) it displays upto 4 elements. i think it's something to do with the last For loop. Can u please help me with that?
sure i will help you please edit your question with latest code
i got it right.. :p it was just a silly mistake.. thank you very much.. owe u big time.. i was having a wrong idea about Array rows & columns And thank you guys for helping me out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.