import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
/**
This class encrypts files using the Caesar cipher.
For decryption, use an encryptor whose key is the
negative of the encryption key.
*/
public class CaesarCipher
{
private String keyWordString;
private String removeDuplicates(String word){
for (int i = 0; i < word.length(); i++) {
if(!keyWordString.contains(String.valueOf(word.charAt(i)))) {
keyWordString += String.valueOf(word.charAt(i));
}
}
return(keyWordString);
}
/**
Constructs a cipher object with a given key word.
@param aKey the encryption key
*/
public CaesarCipher(String aKeyWord)
{
keyWordString = removeDuplicates(keyWordString);
keyWordString = aKeyWord;
//create the mapping string
//keyWordString = removeDuplicates(keyWordString);
int moreChar = 26 - keyWordString.length();
char ch = 'Z';
for(int j=0; j<moreChar; j++)
{
keyWordString += ch;
ch -= 1;
}
System.out.println("The mapping string is: " + keyWordString);
}
/**
Encrypts the contents of a stream.
@param in the input stream
@param out the output stream
*/
public void encryptStream(InputStream in, OutputStream out)
throws IOException
{
boolean done = false;
while (!done)
{
int next = in.read();
if (next == -1)
{
done = true;
}
else
{
//int encrypted = encrypt(next);
int encrypted = encryptWordKey(next);
System.out.println((char)next + " is encrypted to " + (char)encrypted);
out.write(encrypted);
}
}
}
/**
Encrypts a value.
@param b the value to encrypt (between 0 and 255)
@return the encrypted value
*/
public int encryptWordKey(int b)
{
int pos = b % 65;
return keyWordString.charAt(pos);
}
}
When I am going to run this code it gives me a run time error that says:
Exception in thread "main" java.lang.NullPointerException
at CaesarCipher.removeDuplicates(CaesarCipher.java:15)
at CaesarCipher.<init>(CaesarCipher.java:29)
at CaesarEncryptor.main(CaesarEncryptor.java:29)
Let's say I enter JJJJJJJavvvvvaaaaaaa
and I want it to yield Java and give me the encrypted code. In order to differentiate from the other question that was already asked I need the removeDuplicates method to execute and have the output to print JavaZYWW.....etc. Any help or suggestions? I help would be greatly appreciated