For a programming assignment, I have to encrypt a message entered from the console. What is giving most trouble is writing the method with which to encrypt a message which the user would input into the console. Currently, my encryption method is written like this:
import java.lang.*;
public class EncrypterDecrypter
{
public static final String ORDERED = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
private String key;
public EncrypterDecrypter(String theKey)
{
key = "";
}
public String encrypt(String clearText)
{
String encryptedClearText = "";
for (int i = 0; i <= clearText.length(); i++)
{
int length = clearText.length();
int index = ORDERED.indexOf(length);
encryptedClearText += key.charAt(index);
}
return encryptedClearText;
}
}
I always get a String Index Out of Range -1 error message when I try to run this via my executing class. I've tried many different variations of code, yet nothing has worked. Sometimes, if I change the "index" in the "charAt" method to "length", it would return me the user-inputted string.