Hey guys, me again
I’m trying to write a Caesar cypher program to help me learn and understand arrays, so I have to shift to the next letter from the letter in the message to create the cypher.
Here’s what I have so far
public class CaesarCypher
{
static String encryptTheMessage(String message)
{
String newString = "";
char alphabet [] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for(int i = 0; i < message.length(); i++)
{
char oldChar = message.charAt(i);
newString = newString + alphabet[oldChar + 1];
}
return newString;
}
public static void main(String [] args)
{
System.out.print("Enter a message to encrypt: ");
String userMessage = Console.readString();
String encryptedMessage = encryptTheMessage(userMessage);
System.out.print("The encrypted message is: " + encryptedMessage);
}
}
If you compile it, it gives you an out of bounds exception, but I don’t know where I’m going wrong. I’ve written the problem out on paper and attempted the solution and just can’t get it. So any ideas??
Thx guys
Hauk
