I'm a beginner to Java and I've been doing an assignment for one of my classes. I'm looking for some tips and pointers
in order to discover why I keep receiving the array out of bounds error.
My goal for this assignment is to write a program which will take in both upper and lower case letters then encrypt them, omitting any spaces or punctuation.
In my program, I've used a random number generator so that I always get a new cipher every execution of my program. So, whenever I receive a number from the random generator, I move the original letter that many places along the alphabet then
assign that as the new character and store the value of the random number in an array.
This is the error I am receiving through the Eclipse console window:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 71
at Encryptor.<init>(Encryptor.java:53)
at Test.main(Test.java:14)
and here is my code:
import java.util.Random;
public class Encryptor {
// The size of the alphabet and what is contained within the alphabet in an array
public static final int AlphaSize = 52;
public static final char[] alpha = { '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',
'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' };
/* Arrays which will hold the value of the encrypted and original values of the string input and also an ArrayList to hold the
number of rotations for each character. */
protected char[] encrypt = new char[AlphaSize];
protected char[] decrypt = new char[AlphaSize];
protected int[] numstore = new int[100];
// A random number generator which will be used to generate the number of rotation for each letter.
public Random rand = new Random();
/** A constructor which initialises the encryption and decryption arrays */
public Encryptor()
{
// The encryption.
for (int i=0; i < AlphaSize; i++)
{
int rotate = rand.nextInt(AlphaSize); // Generates a random number for encryption.
encrypt[i] = alpha[ (i + rotate) % AlphaSize ]; // Moves whatever char i will be along 'rotate' places along the alpha.
numstore[i] = rotate; // Adds the value of rotate to the Array for storage
}
// The decryption.
for (int i=0; i < AlphaSize; i++)
{
for (int j=0; j < numstore.length; j++)
{
int rotate = numstore[j];
decrypt[encrypt[i] - rotate ] = alpha[i]; // Reverse of encrypt.
}
}
}
/** The encryption method */
public String encrypt(String s)
{
char[]mess = s.toCharArray();
for (int i=0; i < mess.length; i++)
{
if (Character.isUpperCase(mess[i]) && (Character.isLowerCase(mess[i]))) // Excludes any punctuation.
{
for (int j=0; j < numstore.length; j++)
{
int rotate = numstore[j]; // Finds the random number value used.
mess[i] = encrypt[mess[i] - rotate]; // Encrypts the letter
}
}
}
String secret = new String(mess);
secret.trim();
return secret;
}
/** The decryption method */
public String decrypt(String s)
{
char[]mess = s.toCharArray();
for (int i=0; i < mess.length; i++)
{
if (Character.isUpperCase(mess[i]) && (Character.isLowerCase(mess[i]))) // Excludes any punctuation.
{
for (int j=0; j < numstore.length; j++)
{
int rotate = numstore[j]; // Finds the random number value used.
mess[i] = decrypt[mess[i] - rotate]; // Decrypts the letter
}
}
}
return new String(mess);
}
}
I've tried comparing the error to the values of each ASCII character and changing to an ArrayList all to no avail.
I'd very much appreciate help and tips from anyone.
Thanks!

New Topic/Question
Reply



MultiQuote




|