• February 22nd, 2012, 11:25 AM
knin
I'm a beginner in Java programming. i'm learning from a video tutorial on youtube. i was trying to cre8 array to calcul8 the probability of outcomes on throwing dice. Please tell me where i'm going wrong.

the error message i get is

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator /= is undefined for the argument type(s) float[], float

at arrayprac.probability(arrayprac.java:30)
at arrayprac.main(arrayprac.java:18)

Code Java:

```import java.util.Random; import java.util.Scanner; public class arrayprac { public static void main(String args[]) { Scanner scan= new Scanner(System.in); Random dice=new Random(); int frequency[]=new int[7]; float probability[]=new float[7]; System.out.println("Enter the sample size"); int sample=scan.nextInt(); for(int counter=0;counter<sample;counter++) { ++frequency[1+dice.nextInt(6)]; } System.out.println("Face\tFrequency\tProbability"); probability(probability,sample); for(int face=1;face<=6;face++) { System.out.println(face + "\t" + frequency[face] + probability[face]); }   }   public static void probability(float x[], float s) { for(int element=0;element<=x.length;element++) { x/=s;   } } }```
• February 22nd, 2012, 11:33 AM
KevinWorkman

But the compiler error is telling you exactly what the problem is. You're trying to use /= on an array of floats. You can't do that. Perhaps you meant to use an index operator?

Looks something like this:

arrayName[index]
• February 22nd, 2012, 11:47 AM
knin