0

I'm pretty much a noob to programming but i have researched all over the place and cant find an answer. im using eclipse and every time i run my program it says:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at computer.guess(game1player2.java:24)
    at game1player2.main(game1player2.java:39)

Here's my code:

import java.util.Scanner;

class computer{

    int g = 0;
    int[] compguess = new int[g];


    void guess(){

        int rand;
        while(0 < 1){

            int i;
            rand = (int) Math.ceil(Math.random()*10);
            for (i = 1; i < compguess.length; i++){

                if(rand == compguess[i]){
                    break;
                }
            }
            if(i > compguess.length){
                g++;
                rand = compguess[g];
                System.out.println(compguess[compguess.length]);
            }
        }
    }
}

public class game1player2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        computer computer1 = new computer();
        for(int a = 0; a < 2; a++){
            computer1.guess();
            for(int n = 0; n <= computer1.compguess.length; n++)
                System.out.println(computer1.compguess[n]);
            }
    {
        input.close();
    }
    }
}

i am now really confused, i am trying to make a computer generate a random number 1-10, but if it is already in the array generates another one.

2
  • n <= computer1.compguess.length should be < instead. <= means you're doing 0->n, which is actually n+1 iterations. e.g. 0,1,2,3,4 is a 5-item array, but you're actually iterating 0,1,2,3,4,5 and ending up tryign to access 1 element past the end of the array. Commented Oct 16, 2013 at 20:07
  • Why not while(true) instead of while(0 < 1)? Commented Oct 16, 2013 at 20:16

5 Answers 5

3
int g = 0;
int[] compguess = new int[g];

Your array is size 0, so you have no valid entries.

Sign up to request clarification or add additional context in comments.

1 Comment

also, your g++ is useless as arrays cannot be resized at runtime. this means your array stays size 0 and never changes
1
  1. Since you initialized g as zero, your array compguess has a length of zero. Next when you enter your for loop you assign 1 to i which will allow you to enter into the if condition at the end of guess which will try to access element compguess[1] but this cannot exist because the array is of size zero.

  2. You will run into problems if you do not correct the following.

    Change: for(int n = 0; n <= computer1.compguess.length; n++)

    To: for(int n = 0; n < computer1.compguess.length; n++)

    If your array length is 8 then the last item in the array will be index 7, but the <= tells the loop to grab item index 8.

Comments

0

Your compguess has a length of 0, and you are starting your for loop with i = 1, wich is already greater than 0.

1 Comment

Sure, but that means there's no array indexing in that part of the code either.
0

compguess is a zero-length array. If you try to index it, you will fall out of the array and hence the ArrayIndexOutOfBoundsException

Comments

0

If your intent is to make the array longer and add a new item to the end of it, you can't do that. I'm guessing that this is what you were trying to do here:

rand = compguess[g]; 

First of all, if the language did allow it, you'd want to write it the other way:

compguess[g] = rand;

because you're trying to put a value into a new element of the array, not read from the array. This would actually work in some languages (JavaScript, Perl, others). In Java, however, when you create an array object with something like new int[], the size is fixed. You can't make it longer or shorter.

You probably want to use an ArrayList, which does let you create an array that you can make longer. See this tutorial.

Comments

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.