Can someone tell me why im getting the following error from the code below?? arr
ID: 3852818 • Letter: C
Question
Can someone tell me why im getting the following error from the code below??
array2.java:9: ']' expected
int frequency [10];
CODE
import java.util.Random;
public class array2
{
public static void main( String args[] )
{
Random randomNumbers = new Random(); // random number generator
int frequency [10];
for(int i=0; i<10; i++)
frequency[i]=0;
int number; // stores most recently generated value
// summarize results of 6000
for ( int col = 1; col <= 6000; col++ )
{
number = 1 + randomNumbers.nextInt( 10 );
frequency[number]++;
}
System.out.println( "number Frequency" ); // output headers
for(int i=0; i<10; i++)
System.out.printf(" %d %d",(i+1),frequency[i]);
int even = 0, odd = 0;//stores even or odd totals
for(int i=0; i<10; i++)
{
if(i%2==0)
odd +=frequency[i];
else even += frequency[i];
}
System.out.println(" heads: "+even+" tails: "+odd);
}
}
Explanation / Answer
Because you have written the array declaration in a wrong way. In java we must declare the array as like below
int frequency[] = new int[10] ;
This will compile the code without any errors. We must follow the certain rules while dealing with this things as these works only in the way they have designed or written up i.e. we always need to write the code syntactically correct.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.