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

Thread: help with array

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default help with array

    my assignment is to code a program to simulate the rolling of 2 dice. I have most of it set but when I run it and have it print out how many times each number (2-12) was rolled, it displays the numbers 1 off. for instance it will display the number of 4s rolled next to the 5s.

    here it is
    import java.util.Scanner;                                         //allows info to be inputted//
    import java.util.Random;                                          //opens random number generator//
     
    public class RollTheDiceWithArray13
     
     
     
    {
     
          public static void main(String[] args)
          {
          Scanner stdIn = new Scanner(System.in);                     //telling program that information will be gathered from what the user inputs//
          System.out.println("Welcome to the CS1400 Dice Simulator"); //Welcome user//
          int numberRolls;                                            //Preparing to receive int//
          int numberAsterisk = 0;
          System.out.print("How many times would you like to play?"); //Asking for the int//
     
     
          numberRolls = stdIn.nextInt();                              //storing the int//
     
             int[] tally = new int[13];                               // array for count of rolled dice
     
             for(int i=0; i<numberRolls; i++)                         //setting up conditions to repeat actions//
             {int dieOne = new Random().nextInt(6) + 1;               //setting up variable for first die//
             int dieTwo = new Random().nextInt(6) + 1;                //setting up variable for second die//
             int diceTotal = dieOne+dieTwo;                           //setting up variable for sum of the dice//
             System.out.println("score on the roll " + diceTotal);    //printing results(mainly for my benefit to see if the program was working)//
     
             tally[diceTotal]++;                                      //Keeping rolling stotal of score on dice.//
     
     
             }
     
             for (int i = 2; 1 < tally.length; i++)
     
             System.out.println((i+1) + ": " + tally[i]);             //printed tally counts//
     
     
     
     
     
     
     
     
     
     
          }
     
     
     
    }

    here is the error
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
    at RollTheDiceWithArray13.main(RollTheDiceWithArray13 .java:36)


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: help with array

    Look at these 2 lines and think very carefully:
    int diceTotal = dieOne+dieTwo;
    tally[diceTotal]++;
    What values can diceTotal have? What is possible and impossible?
    How many elements are you supposed to store in your array? What would be the first and what would be the last valid index?

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: help with array

    what's possible is 2-12 impossible would 1, and anything >12
    11 elements stored in the array 2-12
    first would be 0 and last would be 11

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: help with array

    If the first one is 0 and the last one is 11 you got 12 elements, but you want to store 11 elements.
    So the first position is 0 and the last position is 10. (thats an array of size 11)

    If you are given an integer of value somewhere between 2 and 12 and you want to generate a unique index between the range of 0 to 10, what do you do?

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: help with array

    cry?
    i set the array to 11
    but then i get this error
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: help with array

    Because you are still trying to access invalid array indices.

    You use the variable diceTotal as an index into the array, but you arlready said it yourself:
    what's possible is 2-12
    But if 10 is the last entry in your array and diceTotal has a value of 11 or 12 (you said it, its possible) then of course you get an exception.

    You have to change the way you access the array. First step is to calculate the correct index.
    So the question is:
    If you are given an integer of value somewhere between 2 and 12 and you want to generate a unique index between the range of 0 to 10, what do you do?

  7. The Following User Says Thank You to Cornix For This Useful Post:

    Heri623 (July 2nd, 2014)

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with array

    Another way to handle it would be to use the sum of the die without changing their sum to be 0 based (-2) and to make an array large enough to be indexed by 12 and leave the first two slots in the array unused. This would keep the code from continually having to subtract 2 for indexing and adding 2 for display.
    Just a thought.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: help with array

    Got that part working, thanks. Now I'm trying to print percentages, but I can't get it to go through the second for loop. j starts at 0, and numberAsterisk is a percentage, so shouldn't it print something? When I debug, it doesn't even go through that part.

    import java.util.Scanner;                                         //allows info to be inputted//
    import java.util.Random;                                          //opens random number generator//
     
    public class RollTheDiceWithArray13
     
     
     
    {
     
          public static void main(String[] args)
          {
          Scanner stdIn = new Scanner(System.in);                     //telling program that information will be gathered from what the user inputs//
          System.out.println("Welcome to the CS1400 Dice Simulator"); //Welcome user//
          int numberRolls;                                            //Preparing to receive int//
          int numberAsterisk = 0;                                     //setting up variable for number of asterisk
          System.out.print("How many times would you like to play?"); //Asking for the int//
     
     
          numberRolls = stdIn.nextInt();                              //storing the int//
     
             int[] tally = new int[13];                               // array for count of rolled dice
     
             for(int i=0; i<numberRolls; i++)                         //setting up conditions to repeat actions//
             {int dieOne = new Random().nextInt(6) + 1;               //setting up variable for first die//
             int dieTwo = new Random().nextInt(6) + 1;                //setting up variable for second die//
             int diceTotal = dieOne+dieTwo;                           //setting up variable for sum of the dice//
             //System.out.println("score on the roll " + diceTotal);  //printing results(mainly for my benefit to see if the program was working)//
     
             tally[diceTotal]++;                                      //Keeping rolling stotal of score on dice.//
     
     
             }
     
             for (int i = 2; i < tally.length; i++)
             {
             numberAsterisk = (int) ((tally[i] / numberRolls) * 100);
     
     
                for (int j = 1; j < numberAsterisk; j++)
                {
     
                System.out.print((i) + ": " + "*");             //printed percentages//
     
                }  
             }    
     
          }
     
    }

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with array

    For debugging, print out the value of numberAsterisk so you can see what the code is doing.


    One problem is a for statement does NOT have {}s enclosing the statement(s) that are supposed to be inside of the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Heri623 (July 2nd, 2014)

  12. #10
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: help with array

    all 0s. is there a problem with this line?
    numberAsterisk = (int) ((tally[i] / numberRolls) * 100);

    Shouldn't that take the element at position i in the array, divide by number of rolls, then multiply by 100 to get a percentage?

    Not sure what you mean about brackets. It doesn't have them as is, or it shouldn't have them?

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with array

    about brackets
    The {}s are missing. The code should have them.

    all 0s.
    It is doing integer divides: 3/4 = 0
    vs floating point: 3/4.0 = 0.75
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Heri623 (July 1st, 2014)

Similar Threads

  1. Blue Pelican Java- Array of Hope; char array for loops?
    By Draco579 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 11th, 2017, 05:36 AM
  2. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  3. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  4. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM