I have the code done for the rest of my program but I can not get the asterisks in my histogram to print can you help??

Thanks In Advance!!

Here is a copy of my code (driver and class).

 public class diceSimulation
{
    public static void main(String[] args)
    {
      char choice;          //user's choice of what he/shewants to do
      boolean done = false;  //flag that tells whether user wants to quit

      diceSimulation2 diceSimulation = new diceSimulation2();
      System.out.println("Welcome to my dice throwing simulation");

      do
      {
        System.out.println("Options: (n)ew simulation, (a)dditional rolls," +
          " (p)rint report, (q)uit");
        System.out.print("Enter n, a, p, or q ==> ");
        choice = Input.readChar();

        switch (choice)
        {
          case 'n': case 'N':
           diceSimulation.newSimulation();
           break;
          case 'a' : case 'A':
           diceSimulation.additionalRolls();
           break;
          case 'p': case 'P':
           diceSimulation.printReport();
           break;
          case 'q': case 'Q':
           done = true;
           break;
          default:
           System.out.println("Invalid selection.");
        }// end switch
     }while (!done);
}
}// end main

This is the Class

public class diceSimulation2
{
  final int numofRolls = 13;
  int rolls;
  int [] frequency = new int [numofRolls + 1];

  //New Simulation
 public void newSimulation()
 {
   int n = 12;

   for (int i=2; i<n; i++)
   {
     frequency[i] = 0;
   }

   System.out.print("How many dice rolls would you like to simulate?");
   rolls = Input.readInt();
 }

/************************************************************************************************/
 //Roll the dice additional times
public void additionalRolls()
{

    int aRolls;


System.out.print("How many additional dice rolls would you like to simulate?");
    aRolls = Input.readInt();
    rolls  += aRolls;
    results(aRolls);
}

/*************************************************************************************************/
//Get Results
public void results(int totalRolls)
{
    int sum;
    int i;

    for(i=0; i<totalRolls; i++)
    {
        sum = (int) (Math.random()*6) +1
        + (int) (Math.random()*6) +1;
        frequency[sum]++;
    }
}

 /************************************************************************************************/
 //Print Histogram
public void printReport()
{

    String rollSpace = " ";
    float tRolls;
    int numofAsterisks;
    double onePercent = rolls / 100 ;
    int sum;
    int i;

    System.out.println("DICE ROLLING SIMULATION RESULTS");
    System.out.println("Each '*' represents 1% of the total number of dice rolls.");
    System.out.println("Total number of dice rolls = " +rolls);

    for(sum=2; sum<=12; sum++)
    {
      rollSpace = "";
      //tRolls = (float) frequency[sum]/rolls;
      //numofAsterisks = Math.round(tRolls * 100);
      numofAsterisks = (int) (frequency[sum] / onePercent);
      System.out.println(sum + ": ");

      //if(sum<10)
      //{
        //rollSpace += "";
      //}
      //rollSpace += sum+":";
      for(i=0; i<numofAsterisks ; i++)
      {

          //rollSpace += "*";
          System.out.print("*");

      }

   }

}
}

Dani AI

Generated

Quick diagnosis: the program prints "Total number of dice rolls" but no stars because the frequency bins are never filled for a brand‑new simulation. The existing code calls results(...) when adding rolls, but not when starting a new simulation, so frequency[...] stays at zero. Also the reset loop in newSimulation() stops short of the 12 bin, so a repeat/new run may leave stale counts.

Concrete fixes (minimal and safe)

  • In newSimulation() clear all bins for the valid sums (2 through 12 inclusive). You can use Arrays.fill(...) or a loop that uses <= 12.
  • After reading the number of rolls, actually perform the simulation for that many rolls (call your rolling routine with that count).
  • Make the percent → asterisks conversion use floating point (avoid integer division). Compute percent = (frequency * 100.0) / rolls and round to an int, or compute fraction = frequency / (double)rolls and multiply by 100.
  • Print the label and stars on the same line (print the label, then the stars, then println to finish the row) so the histogram looks right.
  • Guard against rolls == 0 to avoid divide‑by‑zero.

Minimal example of the key change (illustrates intent, not a full method):

/* after reading rolls in newSimulation() */
for (int i = 2; i <= 12; i++) frequency[i] = 0;
results(rolls);   // actually simulate and fill frequency[]

Quick debugging tips

  • Add temporary prints that show frequency[2..12] and numOfAsterisks before drawing stars to confirm values.
  • Try a small test (e.g., 12 or 60 rolls) so expected asterisk counts are obvious.
  • As suggested, check numOfAsterisks first — that will point you to whether the problem is counting or the display.

These changes address the "no asterisks" symptom directly and also harden the histogram math and formatting.

Recommended Answers

All 5 Replies

Use code tags.
Tell exactly what's going on and what's going wrong.

I'm not going to read through a hundred lines of malformated code to look for a typo or a missing statement, point me to the correct 10 lines or so and I may take a look.

ok to my understanding the asterisks are suppossed to print during this for loop and I am not getting any asterisks can anyone help me??

for(i=0; i<numofAsterisks ; i++)
{

//rollSpace += "*";
System.out.print("*");

Check out the value of numofAsterisks.
Most likely it's 0...

ok I set numofAsterisks to = 0.
and this is the output I am getting:
(which is the same as I was getting before)


Welcome to my dice throwing simulation
Options: (n)ew simulation, (a)dditional rolls, (p)rint report, (q)uit
Enter n, a, p, or q ==> n
How many dice rolls would you like to simulate?200
Options: (n)ew simulation, (a)dditional rolls, (p)rint report, (q)uit
Enter n, a, p, or q ==> p
DICE ROLLING SIMULATION RESULTS
Each '*' represents 1% of the total number of dice rolls.
Total number of dice rolls = 200
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Options: (n)ew simulation, (a)dditional rolls, (p)rint report, (q)uit
Enter n, a, p, or q ==>

What I meant was that you should check the value of the variable because it probably is 0 and therefore nothing will get printed...

Either that or your frequencies are all so low that (when divided by 2 in your case) the result is less than 1 so gets rounded to 0 and again nothing is printed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.