Errors

I keep getting these errors when I run this code

errors: Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException: 8

at ArrayCheck.isFull(ArrayCheck.java:9)

at InputRandom.main(InputRandom.java:24)

import java.util.Random;

import java.io.*;

publicclass InputRandom

{

publicstaticvoid main (String[]args)throws IOException

{

BufferedReader console =new BufferedReader(new InputStreamReader(System.in));

System.out.println("How many nodes does the graph have?");

String input = console.readLine();

int x = Integer.parseInt(input);

int i = 1;

Random generator =new Random();

int r = generator.nextInt(2);

int count = 0;

//count++;

//System.out.print(count);

int[] array =newint[x];

//System.out.println(r);

ArrayCheck arrayCheck =new ArrayCheck();

// System.out.println(arrayCheck.isFull(array));

while(arrayCheck.isFull(array)==false)

{

if(i==1 && r==0)

{

i=x;

count = count;

array[i-1]=i;

count=count+1;

System.out.println(i);

//arrayCheck.isFull(array);

System.out.print(count);

// r = generator.nextInt(2);

}

if(i==x && r==1)

{

i= 1;

count=count;

count=count+1;

array[i-1]=i;

//arrayCheck.isFull(array);

System.out.print(i);

}

elseif(i==1 && r==1)

{

i=i+1;

count=count+1;

array[i-1]=i;

//arrayCheck.isFull(array);

System.out.print(i);

}

if(i > 1 && r == 0 )

{

i = i-1;

count=count+1;

array[i-1]=i;

//arrayCheck.isFull(array);

r = generator.nextInt(2);

}

elseif (i > 1 && r==1 && i!=x)

{

i=i+1;

count=count+1;

array[i-1]=i;

// arrayCheck.isFull(array);

r = generator.nextInt(2);

}

}

}

}

[3690 byte] By [simmasterp1a] at [2007-11-26 13:42:27]
# 1
Yes, so you have an array reference where the index provided is outof bounds for the array. Where does the exception tell you thisis occurring? Does it relate in any way to what you've shown us?(Hint: no). How are we expected to help?
es5f2000a at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 2

Its telling me that the error is in this code

public class ArrayCheck

{

//public static void main(String [] args)

public int[] array;

public boolean isFull(int[] array)

{

for (int i = 0; i<=array.length; i++)

{

if (array[i] == 0)

{

return false;

}

}

return true;

}

simmasterp1a at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 3
Why did you start a new Thread for this? http://forum.java.sun.com/thread.jspa?threadID=5119744&messageID=9415547#9415547As the error indicates, the problem is in ArrayCheck.isFull() for which you have not shown the code.
jbisha at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 4
I have posted the code
simmasterp1a at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 5

> Its telling me that the error is in this code

>

> >

>

> public class ArrayCheck

> {

> //public static void main(String [] args)

> public int[] array;

> public boolean isFull(int[] array)

>{

>for (int i = 0; i<=array.length; i++)

>{

> if (array[i] == 0)

> {

> return false;

> }

>

>

> }

> return true;

> }

>

>

Much better.

Array elements start at 0 and progress to the array length - 1.

Example

String[] s = new String[3];

Has elements 0, 1, and 2.

Run through you code on paper/mind for the same size array and see if you can see the problem.

jbisha at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 6

we'll have to see how your ArrayCheck isFull method is implemented. I'm assuming it's your own custom class.

I would also suggest you make isFull a static method since it looks like it just checks if an array is full or not. There's no need for creating an instance of that object just to call this method. It should belong to the class.

Also, while there's nothing "wrong" with your while condition

while (arrayCheck.isFull(array)==false)

I would just writ eit as

while (!arrayCheck.isFull(array))

maybe it's just me, but it's more readable that way, and I rarely, if ever, see people write it like the way you have it at the moment.

youaresofakingwetoddeda at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 7
> I have posted the codeYou need to post the code for ArrayCheckand you need to tell us what the input was that you gave variable x which tells us the range of the array. Apparently it's not 8 since it says it's out of bounds at 8th index.
youaresofakingwetoddeda at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 8
> I have posted the codeI see it now, it was not there when I posted.See my reply #5.Message was edited by: jbish
jbisha at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 9
ok i see the code now..how weird didnt see it earlier.
youaresofakingwetoddeda at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 10

Ok first of all your for loop should look either like this

for (int i = 0; i < array.length; i++)

or like this

for (int i = 0; i <= array.length-1; i++)

Most people use the former (AFAIK). Also, reconsider making the method static so you can access it via the class and not having to creating an instance of that object.

youaresofakingwetoddeda at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 11
Oh yeah, second of all, your method is somewhat flawed. what if your array actually have a value that is 0?i.e.3, 4, 0, 5, 8,...,that means it thinks it's full after looking at index 2 but it might not be full (unless the legit values are > 0).
youaresofakingwetoddeda at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 12

> Most people use the former (AFAIK). Also, reconsider

> making the method static so you can access it via the

> class and not having to creating an instance of that object.

Sounds like a good idea. Also, the "array" field of that class is unneeded--it is never used.

doremifasollatidoa at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...
# 13
> that means it thinks it's full after looking at index> 2 but it might not be full (unless the legit values> are > 0).@OP: might also be faster/prudent to check from last index of the array.
CodeOnFire-againa at 2007-7-8 0:00:17 > top of Java-index,Java Essentials,Java Programming...