Results 1 to 3 of 3
  1. #1
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default Shifting characters in array

    Alright I'm trying to shift the chars in my array to the right but my code only makes it move to the left and I have no idea how to make it go right. Please help.

    Java Code:
    import java.util.Scanner;
    
    public class RotateArray
    {
        
        // Create a scanner to read from keyboard
        Scanner scanner = new Scanner (System.in);
    
        /**
         * Main method to allow execution from the command line
         */    
        public static void main(String[] args)
        {
            RotateArray instance = new RotateArray();    // default constructor
            
            instance.start();
            
        }
        
        /**
         * declare the arrays, prompt the user for the amount of times to rotate, rotate
         * the arrays, and finally print them out.
         */
        public void start() 
        {
            //*******************************Part 1***********************************
            char[] arrayToRotate = {
                '#', 'G', 'N', 'U', 'A', 'H', 'O', 'V', 'B', 'I', 'P', 'W', 'C', 'J', 
                'O', 'X', 'D', 'K', 'R', 'Y', 'E', 'L', 'S', 'Z', 'F', 'M', 'T' 
            };  
            
            //prompt for the amount to rotate
            System.out.println("ROTATES TO THE LEFT, NOT TO THE RIGHT");
            System.out.println("Enter an amount by which to rotate the array");
            int amount = scanner.nextInt();
            System.out.println();
            
            System.out.println("Original array is:");
            printArray(arrayToRotate);
            
            System.out.println("For 2 points, rotated array is:");
            rotateArray(arrayToRotate, amount);
            printArray(arrayToRotate);
            System.out.println();
          
        }// end start()
    
        /**
         * rotates the elements in an array amount times to the right.  The last
         * element wraps around to the first element
         */
        public void rotateArray(char[] array, int amount) 
        { 
            for( int j=0; j<amount; j++) {
                char a = array[0];
                int i;
                for(i = 0; i < array.length-1; i++)
                array[i] = array[i+1];
                array[i]= a;
            }
                
            
        }//end rotateArray()
        
        
        /**
         * send this method an array and it will print it out like A B C D...
         */
        public void printArray(char[] array) {
            //loop through the array
            for(int x=0; x<array.length; x++) {
                //display each index
                System.out.print(array[x] + " ");
            }
            System.out.println();
        }
        
    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Java Code:
        public void rotateArray(char[] array, int amount) 
        { 
            for( int j=0; j<amount; j++) {
                char a = array[0];
                int i;
                for(i = array.length-1; i > 0; i--)
                array[i] = array[i-1];
                array[i]= a;
            }
                
            
        }//end rotateArray()
    should fix it. Generally whenever you have something going one direction and you want it swapped just reverse your loops. All i did was make your inner loop start at the highest value and go down instead of starting at 0 going up
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    Thanks for your help. Your code worked but it didn't wrap around to the beginning but I sorted that out. Thanks again :)

Similar Threads

  1. [SOLVED] Shifting an array
    By VeasMKII in forum New To Java
    Replies: 2
    Last Post: 02-04-2009, 07:18 PM
  2. Counting characters
    By Tiff89 in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 10:21 AM
  3. replacing characters???
    By manda147 in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 09:19 AM
  4. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 03:38 AM
  5. special characters
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 02:28 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
  •