Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2009, 10:56 PM
Member
 
Join Date: Jan 2009
Posts: 24
Rep Power: 0
Mayur is on a distinguished road
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.

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();
    }
    
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-24-2009, 11:14 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-24-2009, 11:19 PM
Member
 
Join Date: Jan 2009
Posts: 24
Rep Power: 0
Mayur is on a distinguished road
Default
Thanks for your help. Your code worked but it didn't wrap around to the beginning but I sorted that out. Thanks again
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shifting an array VeasMKII New To Java 2 02-04-2009 07:18 PM
Counting characters Tiff89 New To Java 10 12-12-2008 10:21 AM
replacing characters??? manda147 New To Java 2 11-29-2008 09:19 AM
Removing characters kDude New To Java 3 12-03-2007 03:38 AM
special characters ravian New To Java 2 11-16-2007 02:28 PM


All times are GMT +2. The time now is 08:57 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org