1

I'm currently doing a program where I have to encrypt a string typed in by the user. I can randomise the letters and let a = c (randomly generated letter) etc... but the one thing I can't seem to do is if there's a space in the string given by the user. So if the user types in "Encrypt this string" , I will be given an error. How can I put the space character in my array so I can change it when it is entered?

    char [] arrayAlphabet;
    arrayAlphabet = new char [26];
    for (int i=0; i<26; i++)
    {
        arrayAlphabet[i] = (char)('a' + i);
    }


   public static char [] createCipher(char [] arrayAlphabet, char [] cipherAlphabet)
{
    List<Character> chars = new ArrayList<>(26);
    for (char c = 'a'; c <= 'z'; c++)
    {
        chars.add(c);
    }
    Collections.shuffle(chars);

    for (int i = 0; i<26; i++)
    {
        cipherAlphabet[i] = chars.get(i);
    }
    return cipherAlphabet;  

Thank you, any help is appreciated.

Herofire
  • 71
  • 7

1 Answers1

2

Instead of a for loop with c = 'a' and c <= 'z', why not use String's toCharArray() instead:

public static char[] createCipher() { // You weren't using your parameters, so I removed them. You can add them back if you want
    char[] chars = "abcdefghijklmnpoqrstuvwxyz ".toCharArray(); // Add more characters to this string if you need them
    return shuffleArray(chars);
}

public static char[] shuffleArray(char[] array) {
    Random rnd = new Random();
    for (int i = array.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        char temp = array[index];
        array[index] = array[i];
        array[i] = temp;
    }
    return array;
}

(you'll also need to import java.util.Random for this to work)

What the "...".toCharArray() does is it takes the string with the characters you want, and, well turns them into a char array ;). The shuffleArray() is a general implementation of the Fisher-Yates shuffle specifically for char[], which is probably going to be faster than converting char[] to List<Character> and back again. (implementation from https://stackoverflow.com/a/1520212/837703)

(Note that character-swapping is not a good encryption in general, if you actually want to protect some information. It is a good exercise though.)

Community
  • 1
  • 1
  • It says ArrayUtils cannot be resolved, I've imported arrays, lists, arraylists and collections though. – Herofire Jan 01 '15 at 20:44
  • really appreciate the time and help – Herofire Jan 01 '15 at 21:22
  • hey it's actually still not working for me unfortunately. There's an error on the listCipher = new List(chars.length); bit which leads to errors on the rest of the code The error is : Cannot instantiate the type List and listCipher cannot be resolved to a variable – Herofire Jan 01 '15 at 22:27
  • @MichaelVu That's odd. Everything compiled fine for me. I updated the answer again. Hopefully it will work this time. –  Jan 01 '15 at 23:03
  • "listChars cannot be resolved to a variable" For some reason it just doesn't work – Herofire Jan 01 '15 at 23:15
  • Sorry about all that. Could you try again just one more time please :) –  Jan 01 '15 at 23:17
  • Don't apologize haha, you're trying to help me so all I can do is thank you! And there's still an error unfrotunately "The method toArray(T[]) in the type List is not applicable for the arguments (char[])" – Herofire Jan 01 '15 at 23:23
  • @MichaelVu Okay :). I wrote the original in my IDE and then I changed a lot of stuff and I guess I didn't proof-read well enough, causing all the errors to creep in. Anyways I updated my answer and added a faster shuffle. –  Jan 01 '15 at 23:37
  • Finally :D Glad to have helped. –  Jan 01 '15 at 23:58