caustr01
caustr01

Reputation: 81

Char incompatible with string

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);
        }
    }
}

Upvotes: 2

Views: 399

Answers (5)

Naveed Yousaf
Naveed Yousaf

Reputation: 133

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);

}

}

Upvotes: 0

sameer_mishra
sameer_mishra

Reputation: 114

Gave it a quick look ,you might use

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

Upvotes: 1

user3317795
user3317795

Reputation:

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

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

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

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();

Upvotes: 2

StoopidDonut
StoopidDonut

Reputation: 8617

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

To convert it to charArray, use:

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

Upvotes: 1

Related Questions