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

Thread: Trying to compare an int variable to an index in an array

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Trying to compare an int variable to an index in an array

    I have a method that is called that returns the value of 2 dice rolls, one for the computer and one for the player.

    I have an array that has all the possible values of dice rolls.

    I want to compare the values of the rolls to the values in the array and then declare the winner by whoever has the lowest array index value. This where the problem occurs. I tried to compare the value of the score to the array this way ::::: scoreOrder[playerScore] < scoreOrder[compScore] ::::: but this doesn't work.

    The error I receive is: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: and then the value of the score.

    All the comparative logic in my If statement needs to be corrected.



    This is what I have so far:

    import java.util.*;
     
    public class Program08 
    {
    	public static void main(String[] args)
    	{
    		Scanner stdIn = new Scanner(System.in);
     
    		//variables
     
    		int playerScore = 1;
    		int compScore = 1;
     
    		playerScore = diceRoll(playerScore);
    		compScore = diceRoll(compScore);
     
    		int scoreOrder[] = {21,66,55,44,33,22,11,65,64,63,62,61,54,53,52,51,43,42,41,32,31};
     
    		if (scoreOrder[playerScore] > scoreOrder[compScore])
    		{
    			System.out.println("player wins");
    		}
    		else if (scoreOrder[playerScore] < scoreOrder[compScore])
    		{
    			System.out.println("computer wins");
    		}
    		else
    		{
    			System.out.println("tie");
    		}
     
     
    	}
     
    	public static int diceRoll(int x)
    	{
    		double die1 = (int) (Math.random()*6+1);
    		double die2 = (int) (Math.random()*6+1);
    		System.out.println(die1);
    		System.out.println(die2);
    		x = (int) Math.max(die1, die2)*10 + (int) Math.min(die1, die2);
    		System.out.println("roll is: " + x);
    		return x;
     
    	}
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trying to compare an int variable to an index in an array

    This statement: scoreOrder[playerScore], doesn't make sense. The score can not be the index of your array since it varies from 11 to 66, and your array does not contain over 60 items. I think that instead you want to find the index of the score in the array, and for that you'll need a for loop.

    Oh, next time you have a similar error, you'll want to show us with a comment which line is causing the error or exception.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    TCDave (November 18th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Trying to compare an int variable to an index in an array

    Sorry about that. Here is the full error message:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 61
    at Program08.main(Program08.java:19)

    Thanks for the advise. It took a bit of thinking to see what direction you were pointing me towards. Making a for loop and then comparing everything in an if statement solved the problem. Thanks.
    Last edited by TCDave; November 18th, 2012 at 03:30 AM. Reason: grammar

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

    curmudgeon (November 18th, 2012)

  6. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trying to compare an int variable to an index in an array

    You're welcome. Glad you've figured it out, and glad that you really did it on your own. Good show!

Similar Threads

  1. How to compare variable with multiple variable?
    By mharck in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2012, 09:02 AM
  2. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  3. How do I compare between the array of char and array of words !!?
    By bady2050 in forum Collections and Generics
    Replies: 1
    Last Post: May 5th, 2012, 05:36 PM
  4. [SOLVED] Assign a loop to variable to compare strings
    By norske_lab in forum Loops & Control Statements
    Replies: 3
    Last Post: March 6th, 2012, 02:46 PM
  5. Assigning an 'int' from a JTextField to a variable...
    By RiskyShenanigan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 26th, 2011, 04:00 PM