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: Snake Eyes / Craps Dice games (JAVA)

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Snake Eyes / Craps Dice games (JAVA)

    Hello,
    I am fairly new to java, roughly about three to four months in. I am having a problem with my java program. My goal is to request the user to enter how many times they want to roll a pair of dices. Each dice have numbers 1-6. When rolling the dices, I randomly pick a number from each dice and total the number. My goal is to calculate the number of times snake-eyes was rolled which is a total of 2 and total number of times a total of 7 was rolled. Here is my code. I am calling the rollDice method to perform the random pick and calculations. The error I am getting is at the bottom of the code. Any suggestions? Your help will be greatly appreciated.


    package dice;
    import java.util.Scanner;
    import java.util.Random;

    public class Dice
    {
    public static void main(String[] args)
    {
    int numRolls; // number of rolls

    Scanner nord = new Scanner(System.in); // create InputStream
    System.out.print("How many times would you like to roll the two dice? "); // prompt to enter how many times to roll dice
    numRolls = nord.nextInt(); // variable to store number of rolls

    // call rollDice method
    rollDice(numRolls);
    } //end main

    public static void rollDice(int numRolls)
    {
    int d = numRolls;
    int[] diceOne = null; // dice one variable
    int[] diceTwo = null; // dice two variable
    int crapsRolled = 0; // variable to hold how many times 7 was rolled
    int snakeEysRolled = 0; // variable to hold how many times snake eyes was rolled
    int totalDiceAmt; // count dice total
    int[] diceRolls; // variable to store total number of dice rolls
    int[] numArr = {1, 2, 3, 4, 5, 6}; // array to hold numbers on dice

    // create an object from the Random class
    Random r = new Random();

    diceRolls = new int[d]; //create array to hold number of rolls

    // loop through number of times you rolled
    for (int i = 0; i < diceRolls.length; i++)
    {
    diceOne[i] = r.nextInt(numArr.length);
    diceTwo[i] = r.nextInt(numArr.length);

    totalDiceAmt = diceOne[i] + diceTwo[i];

    // loop through number of times 2 was rolled
    if (totalDiceAmt == 2)
    {
    snakeEysRolled++;
    }
    // loop through number of times 7 was rolled
    if (totalDiceAmt == 7 )
    {
    crapsRolled++;
    }
    }

    // print statement
    System.out.println("Sneke Eyes was rolled " + snakeEysRolled + " times");
    // print blank line
    System.out.println();
    System.out.println("Craps was rolled " + crapsRolled + " times");
    } // end roll dice method
    } // end Dice class


    run:
    How many times would you like to roll the two dice? 1000
    Exception in thread "main" java.lang.NullPointerException
    at dice.Dice.main(Dice.java:40)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 4 seconds)


  2. #2
    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: Snake Eyes / Craps Dice games (JAVA)

    Thread moved.

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Snake Eyes / Craps Dice games (JAVA)

    Sorry about that and thanks for providing this information on showing how to correctly post code in the forum. Here is the code I hope I’m posting it correctly this time.

    package dice;
    import java.util.Scanner;
    import java.util.Random;
     
    public class Dice 
    {
        public static void main(String[] args) 
        {
            int numRolls;  // number of rolls
     
            Scanner nord = new Scanner(System.in); // create InputStream
            System.out.print("How many times would you like to roll the two dice? "); // prompt to enter how many times to roll dice        
            numRolls = nord.nextInt(); // variable to store number of rolls
     
            // call rollDice method
            rollDice(numRolls);
        } //end main
     
        public static void rollDice(int numRolls) 
        {   
            int d = numRolls; 
            int[] diceOne = null; // dice one variable
            int[] diceTwo = null; // dice two variable
            int crapsRolled = 0; // variable to hold how many times 7 was rolled
            int snakeEysRolled = 0;  // variable to hold how many times snake eyes was rolled
            int totalDiceAmt; // count dice total
            int[] diceRolls; // variable to store total number of dice rolls
            int[] numArr = {1, 2, 3, 4, 5, 6};  // array to hold numbers on dice
     
            // create an object from the Random class
            Random r = new Random();
     
            diceRolls =  new int[d]; //create array to hold number of rolls
     
            // loop through number of times you rolled
            for (int i = 0; i < diceRolls.length; i++) 
            {
                diceOne[i] = r.nextInt(numArr.length);
                diceTwo[i] = r.nextInt(numArr.length);
     
               totalDiceAmt = diceOne[i] + diceTwo[i];
     
               //  loop through number of times 2 was rolled
               if (totalDiceAmt == 2)
               {
                   snakeEysRolled++;
               }
               //  loop through number of times 7 was rolled
               if (totalDiceAmt == 7 )
               {
                   crapsRolled++;
               }
            }
     
            // print statement 
            System.out.println("Sneke Eyes was rolled " + snakeEysRolled + " times");
            // print blank line
            System.out.println();
            System.out.println("Craps was rolled " + crapsRolled + " times");
        } // end roll dice method
    } // end Dice class

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Snake Eyes / Craps Dice games (JAVA)

    Take a look at this piece of the code:

    // loop through number of times you rolled
    for (int i = 0; i < diceRolls.length; i++) 
    {
        diceOne[i] = r.nextInt(numArr.length);
        diceTwo[i] = r.nextInt(numArr.length);

    Using Eclipse's debugger - diceOne and diceTwo
    can only be null at this location


    What you have is a null pointer access violation. Take a look at
    the documentation about this type of error.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  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: Snake Eyes / Craps Dice games (JAVA)

    Review the tutorial on arrays: and understand the purpose for declaring arrays, initializing arrays, etc.

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Snake Eyes / Craps Dice games (JAVA)

    WOW. Thanks a million for pointing this out, I did not initialize diceOne and diceTwo arrays. After reading the tutorial it showed me what I was doing wrong. I was able to resolve this by adding:

    int[] diceOne = new int[d]; // dice one variable
    int[] diceTwo = new int[d]; // dice two variable


    Thanks again.

Similar Threads

  1. Snake Gamae in java
    By alimoawia582 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2014, 11:24 AM
  2. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  3. [SOLVED] Help with the Snake in a Snake Game
    By godlynom in forum What's Wrong With My Code?
    Replies: 20
    Last Post: September 27th, 2012, 06:41 PM
  4. Snake Game In Java
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2011, 10:00 AM
  5. Snake game in java
    By freaky in forum Object Oriented Programming
    Replies: 0
    Last Post: April 19th, 2010, 11:04 AM