2

I'm just starting java programing and I've been doing really good so far. So the program I am having problems with works just after it goes through there is an error message, yet the program worked as I wanted it to. The program in question is supposed to role a die 1000 times and count how many times each side of the die was rolled out of 1000. its supposed to display this, which it does.

Here is my program: and the error message I put below it. Thanks for any help in advanced!

public class Test
{
     public static void main(String[] args)
     {//create array of 1,000 random numbers
         int[] randomNumbers = new int[1000];

         for(int i = 0; i < randomNumbers.length; i++)
             randomNumbers[i] =1 +(int)(Math.random() * 6);
         { //initialize count
             int[] counts = countInts(randomNumbers);
             displayIntCount(counts);
         }
     }

     public static int[] countInts(int[] ints)
     { //creat new array to hold occurence values
         int[] counts = new int[6];
         for(int i = 1; i <=counts.length; i++)
             for(int j=0;j<ints.length;j++)
                 if(ints[j] == i)
                     counts[i-1]++;  
         return counts;
     }

     public static void displayIntCount(int[] counts)
     {//display the occurrences
         for (int i = 0; i < counts.length; i++)
                 System.out.println("The number 1 occurs "+counts[i]+" times \nThe number 2 occurs "+counts[i+1]+" times \nThe number 3 occurs "+counts[i + 2]+ " times \nThe number 4 occurs " +counts[i+3]+ " times \nThe number 5 occurs " +counts[i + 4]+ " times \nThe number 6 occurs "+counts[i + 5]+ " times");
     }
} 
2
  • 5
    Don't forget the error message. Commented Mar 9, 2014 at 22:43
  • What does the error say? Commented Mar 9, 2014 at 22:44

2 Answers 2

2

You are making mistake in displayIntCount(), I made the change for you:

public class Test
{
     public static void main(String[] args)
     {//create array of 1,000 random numbers
      int[] randomNumbers = new int[1000];

      for(int i = 0; i < randomNumbers.length; i++)
      randomNumbers[i] =1 +(int)(Math.random() * 6);
      { //initialize count
       int[] counts = countInts(randomNumbers);
       displayIntCount(counts);
      }
     }

    public static int[] countInts(int[] ints)
        { //creat new array to hold occurence values
         int[] counts = new int[6];
         for(int i = 1; i <=counts.length; i++)
             for(int j=0;j<ints.length;j++)
                 if(ints[j] == i)
                     counts[i-1]++;  
         return counts;
        }

     public static void displayIntCount(int[] counts)
      {//display the occurrences
         for (int i = 0; i < counts.length; i++)
             System.out.println("The number "+ (i+1) +" occurs "+counts[i]+ " times");
    } 
} 

You have only 6 elements in the array and after running 6 times, you are running out of index!

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

2 Comments

Probably should be "The number "+(i+1) in displayIntCount()
Thank you very much! I tried to upvote you but I need a reputation of like 15 I guess. but when I get there I will upvote you!
1

For clarification, the exception is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Test.displayIntCount(Test.java:38) at Test.main(Test.java:20) Java Result: 1

The problem is in displayIntCount(), you're looping through with i up until the last index in the array - but then trying to access i+1 through to i+5.

You probably want to therefore change the condition to counts.length-5. In this case it seems like the program just works because the exception causes it to bow out and fail to print anything (and since this was a case you shouldn't have hit so didn't want to print anything anyway, the normal output stays ok.)

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.