Results 1 to 3 of 3
  1. #1
    jhurley03 is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Letter-Shifting Program

    I just want to write a simple program that encodes a word or phrase by "shifting" the alphabet by 'x' amount of numbers. I have it so that the program takes input and shifts it the specified amount, but I want it to wrap through the alphabet, instead of printing special characters. So, A-Z or a-z. Any help would be appreciated.

    Here is what I have so far:

    Java Code:
    public class Alphabet {
    
    	public static void main(String[] args) {
    		
    		String phrase;
    		int shift;
    		Scanner keyboard = new Scanner(System.in);
    		char[] charPhrase;
    		int newValue = 0;
    		char newChar = 0;
    		
    		System.out.println("Please enter a word or phrase:");
    		phrase = keyboard.nextLine();
    		
    		System.out.println("Please enter the amount you want me to shift the letters: ");
    		shift = keyboard.nextInt();
    		
    		charPhrase = phrase.toCharArray();
    		
    		if(shift > 0)
    		{		
    				for(int i = 0; i < charPhrase.length; i++)
    				{
    					newValue = (int)phrase.charAt(i) + (shift);
    					newChar = (char)newValue;
    					System.out.print(newChar);
    					
    				}
    		}
    		else
    		{
    			System.out.println("Shift value must be greater than zero.");
    			System.exit(0);
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Letter-Shifting Program

    Are you aware of the % (modulo) operator?

    db

  3. #3
    jhurley03 is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Letter-Shifting Program

    Quote Originally Posted by DarrylBurke View Post
    Are you aware of the % (modulo) operator?

    db
    I've heard of it. Where would I implement it?

Similar Threads

  1. Letter counter program. having trouble understanding it.
    By silverglade in forum New To Java
    Replies: 17
    Last Post: 05-21-2011, 01:06 PM
  2. Shifting method using >>
    By ile4 in forum New To Java
    Replies: 7
    Last Post: 11-09-2010, 06:19 PM
  3. Shifting String Letters
    By DreamNaut in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 08:12 PM
  4. Bit Wise operation: shifting
    By rueter in forum New To Java
    Replies: 1
    Last Post: 06-27-2009, 06:51 AM
  5. [SOLVED] Shifting an array
    By VeasMKII in forum New To Java
    Replies: 2
    Last Post: 02-04-2009, 06:18 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
  •