简体   繁体   中英

Java - Encoding a string / Assigning a random (unused) letter to specific letters

I'm trying to make a cryptogram program in which the letters in a string of text are replaced with other randomly-generated letters. For example, the phrase "Beggars can't be choosers", would look something like "rannlxm zlpo ra zbddmaxm".

My current problem is that the random letter that is being generated for a specific letter is not being applied to other matching letters in the string (All 'g' characters in the original string should be an 'n' (or whichever random letter was generated) in the encoded string). I'm sure that there's a relatively simple solution that I'm missing, so any advice is greatly appreciated.

After passing my char array to my encoder method, I'm trying to iterate through the elements in the array to check for matching letter values. If a matching letter is found, the corresponding index of the encoded character array should be assigned the random letter that was previously generated for the original letter, until the original text string and all of its letters are encoded.

//Method to create an encoded phrase
public static String phraseEncoder(char[] phrase) {
    Random rand = new Random();
    char letter = '*';              
    char randomLetter = '*';
    char[] encodedChars = new char[phrase.length];  //Char array that will hold the encoded letters
    String encodedPhrase = "";
    ArrayList<Character> encryptedChars = new ArrayList<Character>();

    //Iterate through the elements in the array list to encode the characters
    for (int i = 0; i < phrase.length; ++i) {
        randomLetter = (char)(rand.nextInt(26) + 'a');

        //Determine if the character at the current index is a letter
        if (Character.isLetter(phrase[i])) {
            letter = phrase[i];
            encodedChars[i] = randomLetter;

            //Check the rest of the array for matching characters
            for (int j = i + 1; j < phrase.length; ++i) {

                //If a matching letter is found, change it to the previously defined random letter
                if (phrase[j] == letter) {
                    encodedChars[j] = randomLetter;
                }

                //If a matching letter is not found, continue to check the rest of the loop
                else {
                    break;
                }
            }
        }

        //Character at current index is not a letter, add it to the encodedChars array and move to the next element
        else {
            encodedChars[i] = phrase[i];
        }
        encryptedChars.add(randomLetter);
    }
    encodedPhrase = String.valueOf(encodedChars);
    return encodedPhrase;
}

I've tried creating an ArrayList that holds my randomly generated letters, so that no random letters are repeated. My brain is failing me and I can't seem to figure out what I'm doing wrong now.

There are two little issues with your code:

  1. In your inner loop you are incrementing the outer index. It should be j instead of i as in
 for (int j = i + 1; j < phrase.length; ++j) {
   // ...
 }
  1. You are breaking your inner loop prematurely. Either remove the else { break; } else { break; } or replace the break; with continue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM