Reputation: 81
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
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
Reputation: 114
Gave it a quick look ,you might use
"(:mmZ\\dxZmx]Zpgy)".toCharArray()
Upvotes: 1
Reputation:
Your have to add toCharArray cause this is a string and you want char array
char encrypted[]= "(:mmZ\\dxZmx]Zpgy)".toCharArray();
Upvotes: 1
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
Reputation: 8617
"(:mmZ\\dxZmx]Zpgy)"
is a String.
To convert it to charArray
, use:
char encrypted[] = "(:mmZ\\dxZmx]Zpgy)".toCharArray();
Upvotes: 1