2

I have to create a program that decrypts the message :mmZ\dxZmx]Zpgy the encryption method is ASCII code. This should be all that I need but im getting an incompatible type error here:

char encrypted[]= "(:mmZ\\dxZmx]Zpgy)";

I know that technically its a string but I couldn't think of any other way of doing this.. here is my entire code

package decrypt;

public class Decrypt 
{
    public static void decrypt(char encrypted[], int key)
    {
        System.out.println(key + ": ");
        for (int i=0; i < encrypted.length; i++)
        {
            char originalChar = encrypted[i];
            char encryptedChar;
            if ((originalChar -key) < 32)
                encryptedChar = (char) (originalChar - 32 + 127 -key);
            else 
                encryptedChar = (char) (originalChar -key);
            System.out.println(encryptedChar);
        }    
    }

    public static void main(String[] args) 
    {
        char encrypted[]= "(:mmZ\\dxZmx]Zpgy)";    
        for (int i=1; i <=100; i++)
        {
            decrypt(encrypted, i);
        }
    }
}

5 Answers 5

2

String a char array.

char array should consists of individual char elements. Not a whole string.

  char encrypted[]= "(:mmZ\\dxZmx]Zpgy)";

should be either

char encrypted[]= {'(',':',.....remaining elements ..};

or easily

   char encrypted[]= "(:mmZ\\dxZmx]Zpgy)".toCharArray();
Sign up to request clarification or add additional context in comments.

1 Comment

@caustr01 No issues. Happens to every programmer. happy coding.
1

"(:mmZ\\dxZmx]Zpgy)" is a String.

To convert it to charArray, use:

char encrypted[] = "(:mmZ\\dxZmx]Zpgy)".toCharArray();

Comments

1

Your have to add toCharArray cause this is a string and you want char array

char encrypted[]= "(:mmZ\\dxZmx]Zpgy)".toCharArray();

Comments

1

Gave it a quick look ,you might use

"(:mmZ\\dxZmx]Zpgy)".toCharArray()

Comments

0

You are creating an array of character but assigning the string to it.It will give you error but if you create a string just and pass it as argument to the method then your method would look like this

public static void decrypt(String encrypted,int key){
System.out.println(key + ": ");
for (int i=0; i < encrypted.length; i++){
    char originalChar = encrypted.CharAt(i);
    char encryptedChar;
    if ((originalChar -key) < 32)
        encryptedChar = (char) (originalChar - 32 + 127 -key);
    else 
        encryptedChar = (char) (originalChar -key);
    System.out.println(encryptedChar);

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.