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

Thread: Printing A Histogram...

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Printing A Histogram...

    This code is for a dice simulator. Rolling 2 dice and printing out the results in the form of a histogram. The code runs fine. It's just that I don't want to see the number percentages with the asterisks. Does anyone know how to get rid of those numbers before the asterisk?
    Example of my output:
    2: 2 **
    3: 6 ******
    4: 10 **********

    Example of how I want the output to be:
    2: **
    3: ******
    4: **********
    import java.util.*;
    public class DiceSim {
    	public static void main(String[] args) {
    		Scanner Input = new Scanner(System.in);
     
    		int[] frequency = new int [13]; //Declares the array
    		int die, die2;
    		int numbThrows;
    		int asterisk;
    		int total;
    		double fractionOfReps;
     
    		System.out.println("How many dice rolls would you like to simulate? ");
    		numbThrows = Input.nextInt();
     
    		//Roll the dice
    		for (int i=1; i<=numbThrows; i++) {
    			 die = (int)(Math.random()*6) + 1;
                 die2 = (int)(Math.random()*6) + 1;
    	         total = die + die2;  
    	            frequency[total]++;    
    		}
     
    		System.out.println("DICE ROLLING SIMULATION RESULTS " + '\n' + "Each " + '\"' + "*" + '\"' + " represents 1% of the total number" +
    				" of rolls.");
    		System.out.println("Total number of rolls = " + numbThrows);
     
    		//output dice rolls
    		for (total=2; total<=numbThrows; total++)
    		{ 
    			System.out.print(
    					" " + total  + ": " + frequency[total] + " ");
     
    			fractionOfReps = (float) frequency[total] / numbThrows;
    			asterisk = (int) Math.round(fractionOfReps * 100);
     
    			for (int i=0; i<asterisk; i++)
    			{
    				System.out.print("*");
    			}
    			System.out.println();
    		}
    	}
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Printing A Histogram...

    I think the problem lies in this line..
    System.out.print(
    					" " + total  + ": " + frequency[total] + " ");

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Printing A Histogram...

    Can anyone help me?

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Printing A Histogram...

    Quote Originally Posted by Nuggets View Post
    I think the problem lies in this line..
    System.out.print(
    					" " + total  + ": " + frequency[total] + " ");
    Hello Nuggets!
    I'm not sure but what if you remove frequency[total] from that code?

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Printing A Histogram...

    Quote Originally Posted by andreas90 View Post
    Hello Nuggets!
    I'm not sure but what if you remove frequency[total] from that code?
    Removing frequency[total] helped , as it got rid of those numbers. But then it added a 13 to the output. Do you know where that 13 came from?
    Before with frequency[total] -
    2: 2**
    3: 5*****
    4: 6******
    5: 14**************
    6: 11***********
    7: 21*********************
    8: 9*********
    9: 9*********
    10: 9*********
    11: 12************
    12: 2**
    Without -
    2: **
    3: ****
    4: ************
    5: **************
    6: *****************
    7: ****************
    8: **********
    9: ************
    10: ********
    11: ***
    12: **
    13:

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Printing A Histogram...

    Can you post the updated code that gives you this output? And also what you input in the How many dice rolls would you like to simulate? question?

  7. #7
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Printing A Histogram...

    import java.util.*;
    public class DiceSim {
    	public static void main(String[] args) {
    		Scanner Input = new Scanner(System.in);
     
    		int[] frequency = new int [13]; //Declares the array
    		int die, die2;
    		int numbThrows;
    		int asterisk;
    		int total;
    		double fractionOfReps;
     
    		System.out.println("How many dice rolls would you like to simulate? ");
    		numbThrows = Input.nextInt();
     
    		//Roll the dice
    		for (int i=1; i<=numbThrows; i++) {
    			 die = (int)(Math.random()*6) + 1;
                 die2 = (int)(Math.random()*6) + 1;
    	         total = die + die2;  
    	            frequency[total]++;    
    		}
     
    		System.out.println("DICE ROLLING SIMULATION RESULTS " + '\n' + "Each " + '\"' + "*" + '\"' + " represents 1% of the total number" +
    				" of rolls.");
    		System.out.println("Total number of rolls = " + numbThrows);
     
    		//output dice rolls
    		for (total=2; total<=numbThrows; total++)
    		{ 
    			System.out.print(
    					" " + total  + ": ");
     
    			fractionOfReps = (float) frequency[total] / numbThrows;
    			asterisk = (int) Math.round(fractionOfReps * 100);
     
    			for (int i=0; i<asterisk; i++)
    			{
    				System.out.print("*");
    			}
    			System.out.println();
    		}
    	}
    }
    I input 100 dice rolls to simulate. Output is this:
    DICE ROLLING SIMULATION RESULTS
    Each "*" represents 1% of the total number of rolls.
    Total number of rolls = 100
    2: ****
    3: ********
    4: *******
    5: ******
    6: **************
    7: ******************
    8: *****************
    9: ************
    10: *********
    11: ***
    12: **
    13:

  8. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Printing A Histogram...

    for (total=2; total<=numbThrows; total++)
    In the above code total goes up to numbThrows. And then
    fractionOfReps = (float) frequency[total] / numbThrows;
    So when total equals 13 your program stops and you should face an ArrayIndexOutOfBoundsException (don't you?) because frequency[] has 13 indexes (from 0 to 12) and you try to access index 13.

  9. The Following User Says Thank You to andreas90 For This Useful Post:

    Nuggets (March 18th, 2012)

  10. #9
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Printing A Histogram...

    Quote Originally Posted by andreas90 View Post
    for (total=2; total<=numbThrows; total++)
    In the above code total goes up to numbThrows. And then
    fractionOfReps = (float) frequency[total] / numbThrows;
    So when total equals 13 your program stops and you should face an ArrayIndexOutOfBoundsException (don't you?) because frequency[] has 13 indexes (from 0 to 12) and you try to access index 13.
    for (total=2; total<=numbThrows; total++)
    Okay, for the code above, I replaced the numbThrows with a 12.
    for (total=2; total<=12; total++)
    This seemed to fix the problem with the 13 occurring at the end of the output statement. Thanks for the help btw.
    Last edited by Nuggets; March 18th, 2012 at 02:08 PM.

  11. #10
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Printing A Histogram...

    Unfortunately, i can't help you with that. I'm not at all familiar with the Eclipse IDE and how it works.

Similar Threads

  1. [SOLVED] BufferReader, histogram
    By Nhedro in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 22nd, 2012, 08:39 AM
  2. Histogram Java Program
    By djl1990 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 24th, 2011, 06:47 AM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM