Results 1 to 4 of 4
  1. #1
    sd1234 is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Need help with beginner java array program??

    Assignment

    This is a method that will rotate an array by a given number of positions,
    d. If d is positive, the array rotates forward, if d is negative, the array rotates backwards. If d is zero, the array
    is unchanged. For example. if the array a holds the elements 1, 4, 9, 16, 25, 36, after the call rotate(a, -
    2) the values in a are 9, 16, 25, 36, 1, 4.



    here is my code so far.
    Java Code:
    public class Rotater
    {
    
        private static int nums[]={1,2,3,4,5,6,7,8,9};
    
     public static void main(String[] args)
     {
     printOut(nums);
     rotate(nums,3);
     printOut(nums);
     rotate(nums,-4);
     printOut(nums);
     rotate(nums,0);
     printOut(nums);
     
     rotate(nums, -2);
     
    }
    
    public static void printOut(int[] a)
    {
        for(int x=0; x<a.length; x++)
        {System.out.print(a[x]+"    ");
            System.out.println("\n");
        }
        
    }
    
    public static void rotate(int a[], int d)
    {
        int temp=0;
        if(d<0)
        {
            oneLeft(a);
        }
        else if(d>0)
        {
            oneRight(a);
        }
       
    }
    private static void oneLeft(int a[])
    {
        int temp = a[1];
        int temp2;
        
        for(int j=a.length-1; j>=0; j++)
        {	
            if(j==a.length-1)
            {
               temp=a[j-1];
               a[j-1]=a[j];
     
            }
            else if(j==0)
            {	
                a[a.length-1]=a[0];
                    
            }
            else
             {
                temp2=a[j-1];
                a[j-1]=temp;
                temp=temp2;
             }
    
        }
    
    }
    private static void oneRight(int a[])
    {
        int temp = a[1];
        int temp2;
        for(int j=0; j<a.length; j++)
        {
            if(j+1>a.length-1)
            {
                a[0]=a[a.length-1];
            }
            else if(j==0)
            {
               temp=a[j+1];
               a[j+1]=a[j];
    
            }
            else
            {
              temp2=a[j+1];
              a[j+1]=temp;
              temp=temp2;
    
            }
    
        }
    
    }
    }
    Problem

    when i try to run it highlites temp2=a[j-1]; from the program and says "java.lang.ArrayIndexOutOfBoundsException:9"
    Last edited by Norm; 01-31-2012 at 03:35 AM. Reason: added code tags

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Need help with beginner java array program??

    double post ?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Need help with beginner java array program??

    You shouldn't start another thread with the same problem as this one: Java rotater program help

    "java.lang.ArrayIndexOutOfBoundsException:9"
    Your array index is past the end of the array. Remember arrays are 0 based. The last index is the length-1.

    Check your logic to see how the index got too large. If you can't see, add some println statement to print out the values of the variables as the code executes.
    Also print out the size of the array.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,246
    Rep Power
    19

    Default Re: Need help with beginner java array program??

    Let's keep this discussion all in one place. I'm closing this thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Triangle program for beginner
    By shawnx715 in forum New To Java
    Replies: 18
    Last Post: 11-15-2011, 12:40 PM
  2. Help with beginner Java program? Boolean?
    By katiebear128 in forum New To Java
    Replies: 20
    Last Post: 09-30-2011, 06:35 PM
  3. Java Beginner: Can't Figure out Loop Array Issue.
    By CMDR Reservoir in forum New To Java
    Replies: 4
    Last Post: 07-19-2011, 02:30 AM
  4. JAVA Beginner - Simple Program help
    By Logik22 in forum New To Java
    Replies: 13
    Last Post: 07-15-2011, 02:44 PM
  5. Beginner Needs Help w/ Program for School
    By badness in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •