Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: please help me out

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question please help me out

    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)

    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;
     
    		}
    	}
    }

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: please help me out

    Please read the link in my signature on asking questions the smart way, especially about using proper spelling and grammar. Also, a more informative title would have helped you get better help sooner.

    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]
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: please help me out

    Sorry about the spelling shortcuts used in my original post.
    I have a follow up question.
    Aren't the elements considered as regular floats, when in an array?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: please help me out

    Sure, but you can't treat an array of floats exactly the same way you treat a single float. That's like saying a backpack full of books is the same as one book. You have to get a single float from the array, using the index operator I showed you.

    Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    knin (February 22nd, 2012)