Reputation: 55
So I am writing a Lottery
class and class that demonstrates the Lottery
class. I have the work done and it compiles, but when I run it I get
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at LotteryDemo.main(LotteryDemo.java:21)
Here is what I have for the code, any help would be much appreciated!
The Lottery
class:
import java.util.Random;
import java.util.Scanner;
public class Lottery
{
int NUM_DIGITS = 5;
int[] lotteryNumbers = new int[4];
public Lottery(){
Random rand = new Random();
lotteryNumbers[0] = rand.nextInt(10);
lotteryNumbers[1] = rand.nextInt(10);
lotteryNumbers[2] = rand.nextInt(10);
lotteryNumbers[3] = rand.nextInt(10);
lotteryNumbers[4] = rand.nextInt(10);
getLotteryNums();
}
public int numIntsInCommon(int[] picks){
int inCommon = 0;
for (int counter = 0; counter < 5; counter++)
{
for (int index = 0; index < 5; index++)
{
if (lotteryNumbers[counter] == picks[index])
inCommon++;
return inCommon;
}
return inCommon;
}
return inCommon;
}
public String getLotteryNums(){
return lotteryNumbers[0] + ", " + lotteryNumbers[1] + ", " +
lotteryNumbers[2] + ", " + lotteryNumbers[3] + ", " +
lotteryNumbers[4];
}
}
The LotteryDemo
Class:
import java.util.*;
public class LotteryDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[] userNumbers = new int[4];
System.out.println("Please supply your lottery picks. Choose unique numbers between 0 and 9.");
System.out.print("Enter digit 1: ");
userNumbers[0] = input.nextInt();
System.out.print("Enter digit 2: ");
userNumbers[1] = input.nextInt();
System.out.print("Enter digit 3: ");
userNumbers[2] = input.nextInt();
System.out.print("Enter digit 4: ");
userNumbers[3] = input.nextInt();
System.out.print("Enter digit 5: ");
userNumbers[4] = input.nextInt();
Lottery lottery = new Lottery();
System.out.print("Lottery Numbers: " + lottery.getLotteryNums());
System.out.print("Number of matching digits: " + lottery.numIntsInCommon(userNumbers));
if(lottery.numIntsInCommon(userNumbers) == 5)
System.out.print("All 5 match! You have hit the jackpot!");
}
}
Upvotes: 0
Views: 4186
Reputation:
Arrays in Java have instantiation as follows:
type[] name = new type[size]
^
The size represents the number of indexes in an array. As you know, arrays start at zero, thus:
int[] userNumbers = new int[4]
makes an array of:
[ ][ ][ ][ ]
0 1 2 3
userNumbers[4]
therefore does not exist. You need to create userNumber[5]
for five indexes.
Upvotes: 0
Reputation: 43728
You allocate your array with new int[4]
which means it has 4 elements numbered 0 to 3.
When you access the element with index 4 it is out of bounds.
Upvotes: 0
Reputation: 31952
int[] lotteryNumbers = new int[4];
This means the array has 4 elements or lotteryNumbers[0]
.... lotteryNumbers[3]
Thus lotteryNumbers[4] = rand.nextInt(10);
will cause the above exception.
Similarly with userNumbers
in main which is causing the exception.
int[] userNumbers = new int[4];
...
userNumbers[4] = input.nextInt(); // java.lang.ArrayIndexOutOfBoundsException
Upvotes: 1