Reputation: 109
I've been on this assignment for two days now and I'm having such a difficult time! My assignment asks me to create a program that:
It must also show how many times each number out of 10 showed up, what number showed up the most, and if even numbers are heads, and odd numbers are tails, what side of the coin came up the most. Please help me, I've tried writing the code but I'm having such a hard time and I'm really stressed on time!
Here's my code:
import java.io.*;
import java.util.Random;
public class ColCoin
{
public static void main(String[] args) throws IOException
{
//set variables
String timesString;
String run;
int times;
int runNum;
int i = 0;
int x;
//input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//random object
Random r = new Random();
System.out.print("How many times would you like to perform a run through the flips? ");
run = br.readLine();
runNum = Integer.parseInt(run);
do
{
//ask how many times the coin will flip
System.out.print("Please input the amount of times you would like to flip the coin (1-1000): ");
timesString = br.readLine();
//convert String into an integer
times = Integer.parseInt(timesString);
if((times > 1000)||(times < 1))
{
System.out.println("ERROR! Must input an integer between 1 and 1000!");
}
System.out.println("You chose to flip the coin " + times + " times.");
} while((times > 1000)||(times < 1));
for(x=0; x <= runNum; x++)
{
//create array
int flip[] = new int[times];
int countArray[] = new int[i];
//create a new variable
int storeTime;
for(storeTime = 0; storeTime < flip.length; storeTime++)
{
flip[storeTime] = r.nextInt(10) + 1;
// the line above stores a random integer between 1 and 10 within the current index
System.out.println("Flip number " + (storeTime+1) + " = " + flip[storeTime]);
}
//display the counts
for(i=0; i < 10; i++)
{
System.out.println("The occurences of each of the numbers is: ");
System.out.println((i+1) + " appears " + countArray[i] + "times.");
}
}
}
}
It also gave an ArrayIndexOutOfBoundsException error on line 64 and I'm not sure why:
System.out.println((i+1) + " appears " + countArray[i] + "times.");
Thanks in advance!
Upvotes: 0
Views: 194
Reputation: 31
Problem is in this part
int countArray[] = new int[i];
At the time of creation of this array i is Zero thus actually, the array is never filled, so, it's always empty.
Upvotes: 1
Reputation: 178
You are using a dynamic length in the array, but the loop you created to display the output you are using a fixed length (The line below).
for(i=0; i < 10; i++)
Upvotes: 0
Reputation: 2552
the problem is here:
int countArray[] = new int[i];
With this code you create an array with i elements, indexed from 0 to i-1. But in your case int is still 0. so the array has dimension zero (also it seems that you never use that array to input something)
System.out.println((i+1) + " appears " + countArray[i] + "times.");
Here you ask the array to give you the element i!=0, but obviously you cannot because the array is dimensioned as zero.
Upvotes: 2