Hussein Reda AlBehary
Hussein Reda AlBehary

Reputation: 115

How to store Character in a String?

I'm making a word encryption method, so it should take a String plainWord and String key as an arguments.

Now when I tried to get the ciphered Characters and store them in a String word and return that word which made of these Ciphered characters but I can't find a way to do that.

here is the methods I have used :

public static String encryptWord (String plainWord, String key) {
    int pword;
    int k;
    char cipherCH;
    String cipherWord = null;

    for (int i = 0; i < plainWord.length(); i++) {

        pword = Secret_Code_Library.getDigit(plainWord.charAt(i));
        k = Secret_Code_Library.getDigit(key.charAt(i));

        cipherCH = Secret_Code_Library.getLetter((pword+k)%25);
        for (int j = 0; j < plainWord.length(); j++) {
            cipherWord.charAt(j) = Character.toString(cipherCH);
        }
    }
return cipherWord;
}

Now the error is in that line "cipherWord.charAt(j) = Character.toString(cipherCH);" so How should store the ciphered character (cipherCH)in ciphered Word (cipherWord) ?

and here is the getLetter and getDigit methods :

    public class Secret_Code_Library {
     //LETTERS is a reference Class variable contains the alphabetic characters from A to Z in Upper-case    
    public static final char [] LETTERS={'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' };

       /* getLetter is a class method that has one argument which is an integer number.
        * It returns the corresponding ccharacter to the given integer argument.
        */
    public static char getLetter(int digit){
       return  LETTERS[digit];   }


        /* getDigit is a class method that has one argument which is a character.
         * It returns the corresponding number to the given character argument.
         */   
    public static int getDigit(char ch){
        int digit=0;
        switch (ch){
            case 'A': digit=0; break;   case 'B': digit=1; break; case 'C': digit=2; break; 
            case 'D': digit=3; break;   case 'E': digit=4; break; case 'F': digit=5; break;    
            case 'G': digit=6; break;   case 'H': digit=7; break; case 'I':  digit=8; break;           
            case 'J':  digit=9; break;   case 'K': digit=10; break; case 'L': digit=11; break;    
            case 'M': digit=12; break; case 'N': digit=13; break;  case 'O': digit=14; break;    
            case 'P': digit=15; break; case 'Q': digit=16; break;   case 'R': digit=17; break;    
            case 'S': digit=18; break; case 'T': digit=19; break;   case 'U': digit=20; break;
            case 'V': digit=21; break; case 'W': digit=22; break;  case 'X': digit=23; break;    
            case 'Y': digit=24; break; case 'Z': digit=25; break;      
     }// switch
        return digit;
    } 
}

Upvotes: 0

Views: 7815

Answers (1)

JB Nizet
JB Nizet

Reputation: 691705

Two solutions:

  1. create a char array, set each of its elements, and then create a String from this char array (that's what would look the most like what you're trying to do);

  2. use a StringBuilder, append every character, then transform it into a String.

null is not a String. It's nothing. And even if it was, a String is immutable: you can't modify a String. And even if you could, using a method that returns a character in order to set a character wouldn't work. You would need a method such as setCharacterAt(int index, char c).

Note that the most "idiomatic" or "traditional" way is the second one. It's rare to know in advance howmany characters a String will contain, and having a STringBuilder which grows as needed is thus handy.

Upvotes: 1

Related Questions