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 9 of 9

Thread: Array counting problem

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array counting problem

    Hello, so i saw a challenge on youtube which wanted you to generate 100 values from 1-6 and show the percentages I can't figure out what to put into the last bit of code, the diceCount brackets.
    import java.util.Random;
    public class tutorial_23 {
     public static void main(String args[]){
    	  Random dice = new Random();
    	   int number;
    	   int diceCount[]=new int[6];
    	    for(int counter=1;counter<=100;counter++){
    	    	number = 1+dice.nextInt(6);
    	    	 if (number==1)
    	    		 ++diceCount[0];
     
    	    	 else if (number==2)
    	    		 ++diceCount[1];
     
    	    	 else if (number==3)
    	    		 ++diceCount[2];
     
    	    	 else if (number==4)
    	    		 ++diceCount[3];
     
    	    	 else if (number==5)
    	    		 ++diceCount[4];
     
    	    	 else if (number==6)
    	    		 ++diceCount[5];
     
    	    System.out.println("Index\tValue");
    	     for(int arrayCount = 0; arrayCount < diceCount.length; arrayCount++){
    	    	 System.out.println(1+arrayCount + "\t" + diceCount[HERE I GET AN ERROR, DONT KNOW WHAT TO PUT IN HERE!] + "%");
    	     }
     
     
     
    	 //this is the tutorial, with the challenge that creates 100 values from 1-6 and then determines how many 1-6s it counted.        
     
    	    }
     }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Array counting problem

    [HERE I GET AN ERROR, DONT KNOW WHAT TO PUT IN HERE!]

    ...what is the error?
    ...how should we know what to put in there? What should it do?

    My suggestion is to put the code to the side for a minute, and outline how you would solve the problem if you walked into a room and was handed a pencil and paper. How would you keep track of how many of each number was rolled?
    Once you have that outline finished translate it to code. This is how you figure out "what to put in there", you figure out "what it should do, exactly, in step by step order"

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Array counting problem

    dice count wouldnt be an array it would seperate integers for each coun

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Array counting problem

    Hello,
    Your code already has what you want.
    Please try to find out.
    Just think of mathematics.

    Syed.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array counting problem

    A frequency counter is possibly one of the coolest ways to use an array, and no switch statement is required.

    For a random number generator that generates numbers 0 - n
    Create an array to count the frequency of occurrence of each random number of size 'n + 1'
    For each random number generated, increment the corresponding array counter, freqArray[number]++

    No switch or if required!

    Then for the last part of the question, iterate the array (an enhanced for loop would be excellent for this) and print the result of each element of the array.

  6. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Array counting problem

    Guys,
    Actually the values of array itself are what he needed.
    For example, no. of 1's is available in dicecount[0], no. of 2's in dicecount[1] and so on.
    Thats it.
    You can just print somewhat like this:
    System.out.println("count of 1's:"+dicecount[0]);
    System.out.println("count of 2's:"+dicecount[1]);
    ...

    Syed.

  7. #7
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Array counting problem

    I still dont understand why you would initialize dice count as an array.

  8. #8
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Array counting problem

    Try this,
    ...
    Last edited by copeg; August 2nd, 2013 at 12:24 PM. Reason: Removed spoonfed code

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Array counting problem

    @elizabethyang, welcome to the forums. Please read
    http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. Counting and comparing elements in an array
    By TheBestGame in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2012, 05:05 AM
  2. Counting Days problem with object
    By hawkeye10 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 6th, 2012, 09:48 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Counting cells
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 05:04 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM