Getting an array index out of bounds exception error at run.

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1804 Views - Last Post: 09 December 2012 - 07:55 PM Rate Topic: -----

#1 xSouthpaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-December 12

Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:01 PM

As title says im getting an index out of bounds exception error at runtime.

Here is the code.
public class Assign7
{
	public static void main(String[] args)
	{
		int [][] intar = { {10, 12, 13, 14 },
						   {15, 16, 17, 18 },
						   {19, 20, 21, 22 } };
		System.out.println("Here are the values in the array.");
		showArray(intar);
	}

	private static void showArray(int[][] intar)
	{
		for (int row = 0; row<3; row++)
		{
			for (int col = 0; col<4; col++)
			   System.out.print(intar[col][row]);

		}
  	}
}



By the way the output of the program should be 10,15,19,12,16,20,13,17,21,14,18,22.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting an array index out of bounds exception error at run.

#2 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:03 PM

Howdy friend;
public class Assign7
{
	public static void main(String[] args)
	{
		int [][] intar = { {10, 12, 13, 14 },
						   {15, 16, 17, 18 },
						   {19, 20, 21, 22 } };
		System.out.println("Here are the values in the array.");
		showArray(intar);
	}

	private static void showArray(int[][] intar)
	{
		for (int row = 0; row < intar.length; row++) // better to use length (not hardcode)
		{
			for (int col = 0; col < intar[row].length; col++) // better to stay proportionate
			   System.out.print(intar[row][col]); // you switched rows and cols

		}
  	}
}


This post has been edited by k3y: 09 December 2012 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:03 PM

Change "System.out.print(intar[col][row]);" to "System.out.print(intar[row][col]);"
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,986
  • Joined: 08-June 10

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:04 PM

Shouldn't the indexes in your print call be the other way around? Like: intar[row][col]
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:07 PM

The error is coming from
System.out.print(intar[col][row]);
where you inverted col and row,,,

But you should never hardcode values like 3 aqnd 4 as you did... should be
public class Assign7
{
	public static void main(String[] args)
	{
		int [][] intar = { {10, 12, 13, 14 },
						   {15, 16, 17, 18 },
						   {19, 20, 21, 22 } };
		System.out.println("Here are the values in the array.");
		showArray(intar);
	}

	private static void showArray(int[][] intar)
	{
		for (int row = 0; row< intar.length; row++)
		{
			for (int col = 0; col< intar[row].length; col++)
			   System.out.print(intar[row][col]);

		}
  	}
}


Was This Post Helpful? 0
  • +
  • -

#6 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:10 PM

pbl, I think you and I have the same post... Means I must be doing something right =D
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:13 PM

and 3 minues faster than I :)
Was This Post Helpful? 0
  • +
  • -

#8 xSouthpaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 06-December 12

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:14 PM

Thank you everyone for the quick answers. Still an issue though I need the output to print the value across the columns instead of across the rows.
The output that i need is in my first post i edited it.
Was This Post Helpful? 0
  • +
  • -

#9 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:15 PM

View Postpbl, on 09 December 2012 - 09:13 PM, said:

and 3 minues faster than I :)/>

Haha, I just got lucky. However, I am learning these currently as well and was never quite sure why you have to specify
cols < array[row].length


Is it to stay proportionate, or to make sure that each of the columns matches with a row? Any explanation would be great.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:16 PM

so invert the order of the 2 loops.... :nottalkingtoyou:/>

This post has been edited by pbl: 09 December 2012 - 07:20 PM
Reason for edit:: 2 loops not 3

Was This Post Helpful? 0
  • +
  • -

#11 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:19 PM

View PostxSouthpaw, on 09 December 2012 - 09:14 PM, said:

Thank you everyone for the quick answers. Still an issue though I need the output to print the value across the columns instead of across the rows.
The output that i need is in my first post i edited it.

Maybe look into printf()?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:20 PM

View Postk3y, on 09 December 2012 - 10:15 PM, said:

View Postpbl, on 09 December 2012 - 09:13 PM, said:

and 3 minues faster than I :)/>/>/>

Haha, I just got lucky. However, I am learning these currently as well and was never quite sure why you have to specify
cols < array[row].length


Is it to stay proportionate, or to make sure that each of the columns matches with a row? Any explanation would be great.

In Java a 2D array is actually an array of array so every array row can have a different size

int[] array = {
                {1,2,3,4,5,6},
                {0, 2},
                {},
                {1,2,3,4,5,6,7,8,9}
              };


this array has 6, 2, 0, and 9 elements in each of its row

In our case, it was a perfectly square array, but better to use array[row].length for the number of columns in each row

This post has been edited by pbl: 09 December 2012 - 07:21 PM

Was This Post Helpful? 2
  • +
  • -

#13 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:22 PM

So it's like saying: "lets make sure every column, according to what row we are on is effected"
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:24 PM

I am the method showArray
I receive an array as parameter and I have to print it
Nobody told me that that array will be square
so... I'll check each column length
Was This Post Helpful? 0
  • +
  • -

#15 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Getting an array index out of bounds exception error at run.

Posted 09 December 2012 - 07:26 PM

View Postpbl, on 09 December 2012 - 09:24 PM, said:

I am the method showArray
I receive an array as parameter and I have to print it
Nobody told me that that array will be square
so... I'll check each column length

Ah... Much better, now I am not confused. Your skills are intense, thank you for the help.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2