array index out of bound

array index out of bound error after running

Page 1 of 1

2 Replies - 1626 Views - Last Post: 14 April 2009 - 06:59 PM Rate Topic: -----

#1 ragaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-November 07

array index out of bound

Post icon  Posted 14 April 2009 - 12:32 PM

public class SelectionSort
{public static void main(String[] args)
{
int k;
int j;
int m;
int[] N;
k = Console.readInt("Input k: ");
N = new int[k];	

for(int i=0;i<k;i++) 
  {

 N[i] = Console.readInt("Input N["+i+"]: ");	
	}	


  int[]x = N;
  int marker = N.length;
  int max = x[0];

  for(j=0; j<k; j++);
  {
	for(m=0; m<k; m++);
	{
	  if(x[m+1]>=x[m]){
		max = x[m+1];
	  }
	}
  System.out.println("The largest number is:" + max);
  }
}
}
  



after running this code got array index out of bound error
plz help me

Is This A Good Question/Topic? 0
  • +

Replies To: array index out of bound

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: array index out of bound

Posted 14 April 2009 - 02:04 PM

inside your for loops make sure you do i < k.length as opposed to just i < k.

Hope this helps.

Cheers
Was This Post Helpful? 0
  • +
  • -

#3 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: array index out of bound

Posted 14 April 2009 - 06:59 PM

wouldn't you want to do i < k.length()-1, or i < k-1 ?
The last element in each array is x[k-1]. Also, you might want to consider renaming your variables so they are actually meaningful. One letter names make the code not very readable.

-Michael
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1