0

I need to create a program that uses arrays that shuffles a deck back into order. I am supposed to cut the original deck of 52 in half and place the cards in a new pile by alternately placing cards from either pile. The original order 1,2,3,4....49,50,51,52 should come out in the order 1,27,2,28,3...26,52.

 public static void main(String[] args)
  {
    int[] array = new int[52];
    int[] top = new int[26];
    int[] bottom = new int[26];

    for(int i=0; i < 52; i++)
    {
      array[i] = i+1;
    }
    for(int i = 0; i < 26; i++)
    {
      top[i] = array[i];
    }
    for(int i = 0; i < 26; i++)
    {
     bottom[i] = array[i+26];
    }

    System.out.println(shuffle(array, top, bottom));
  }

  public static int[] shuffle(int[] array, int[] top, int[] bottom)
  {
    for(int j = 0; j < top.length; j--)
    {
      array[j*2] = top[j];
      array[j*2-1] = bottom[j];
    }
    return array;
  }

My problem is that I am having an out of bounds exception. I am not sure how to fix this. Even if I do array[j*2+1] I still have this issue.

3
  • At which line do you get the exception? Commented Nov 15, 2013 at 22:20
  • It doesn't tell me. It just says that when I run it. Commented Nov 15, 2013 at 22:21
  • 4
    I assure you, it tells you. Commented Nov 15, 2013 at 22:22

3 Answers 3

6
for(int j = 0; j < top.length; j--)

You start from 0, and then decrement j. So the second iteration will try to access index -1. And in fact, even the first one will, since it tries to access the index j*2-1 which is -1 when j is 0.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, should be array[j*2+1] = bottom[j];
2

change shuffle to:

    public static int[] shuffle(int[] array, int[] top, int[] bottom)
      {
        for(int j = 0; j < top.length; j++)
        {
          array[j*2] = top[j];
          array[j*2+1] = bottom[j];
        }
        return array;
      }

    public static String print(int[] array)
  {
String result="";
    for(int j = 0; j < array.length; j++)
    {
      result=result+top[j];
    }
    return result;
  }

call it like this:

System.out.println(print(shuffle(array, top, bottom)));

8 Comments

no problem, if you have any more questions related post them here ;) also, if this helped you please mark it as the answer
When I print this out it comes out looking like this '[I@1aa3bbc '. Is that because I am printing the array out incorrectly? I print it out like System.out.println(shuffle(array,top,bottom)).
you are printing out the array not the values to printout the values add this function: EDIT: it's inside the answer now after that you could call either System.out.println(print(shuffle(array, top, bottom))); or shuffle(array, top, bottom) System.out.println(print(array));
Should I put that in an if statement so it reads the new array?
Is there any way I can do it without using Strings? I am not really sure what they are.
|
0

On your first iteration when j=0 array[j*2-1] becomes array[-1] and results in exception. Anyways the whole logic is incorrect, there is no reason to decrement j

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.