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

Thread: Can't Fix Array Out of Bounds

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Can't Fix Array Out of Bounds

    Hey guys I made this program that counts the number of times a face value on a die is rolled, based on the user input, but i keep getting a out of bounds exception; HELP
    import java.util.Scanner;
    import java.util.Random;
     
    public class CountDiceRolls
    {
        public static void main(String[] args)
        {
           int numberOfDieRolls = rollADie();
           faceValueOfDie(numberOfDieRolls);
        }
     
        public static int rollADie()
        {
           Scanner input = new Scanner(System.in);
           System.out.print("How Many Times Would You Like To Roll A Die: ");
           int num = input.nextInt();
     
         return num;
        }
     
        public static void faceValueOfDie(int numberOfDieRolls)
        {
            Random random = new Random();
     
            int [] faceVal = new int [numberOfDieRolls];
     
            for(int count = 0; count < numberOfDieRolls; count++)
            {
             int face = 1 + random.nextInt(6);
             faceVal[face]++;
            }
            display(faceVal);
        }
     
        public static void display(int [] faceVal)
        {
         System.out.print("Face\tTimes\n");
         for(int counter = 1; counter <= 6; counter++)
         {
          System.out.println(" " + counter + "\t\t  " + faceVal[counter]);
         }
        }
     
     
    }
    Last edited by JavaPF; October 25th, 2011 at 10:42 AM. Reason: Use highlight tags!


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

    Default Re: Can't Fix Array Out of Bounds

    Which line is giving your the error?
    How many indices does your array have? Print it out to be sure.
    Which index are you trying to access? Print it out to be sure.

    Also, don't forget the highlight tags when posting code.
    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. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Apocalypse (October 26th, 2011)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Can't Fix Array Out of Bounds

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

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

    Apocalypse (October 26th, 2011)

  6. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't Fix Array Out of Bounds

    Thank you very much, helped alot I figured out that I was incrementing it at the wrong time, had to go back and see which operations run in what order so the solution was getting rid of my "face" variable and just making a random number in the array and I had to switch the (++) increment operator from the back to the front and it worked

  7. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't Fix Array Out of Bounds

    Hi,
    I think you need to change your code.
    for <<<
    public static void faceValueOfDie(int numberOfDieRolls)
    {
    Random random = new Random();

    int [] faceVal = new int [numberOfDieRolls];

    for(int count = 0; count < numberOfDieRolls; count++)
    {
    int face = 1 + random.nextInt(6);
    faceVal[face]++;
    }
    display(faceVal);
    }

    >>>
    user faceVal[count] to store like faceVal[count]=face;

    bean factory
    Last edited by abani; December 18th, 2011 at 12:59 AM.

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

    Default Re: Can't Fix Array Out of Bounds

    abani, please read this before posting again: The Problem with Spoon-feeding
    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!

Similar Threads

  1. Out of bounds error, 2d irregular array of integers
    By Farmer in forum Loops & Control Statements
    Replies: 3
    Last Post: August 1st, 2011, 03:55 PM
  2. Array exception out of bounds
    By gabberlt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2011, 08:05 AM
  3. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 06:11 AM
  4. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 11:19 AM
  5. Having problems with String out of bounds errors
    By Bill_H in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2009, 01:47 PM