2

I want to create a method that will use a phrase and a number taken from args. The number form args will shift the letters of the phrase from args the amount of letters the number is.

example javac Caesar.java java Caesar abcd 1

the end result should print bcde

my method is giving me problems.. help?

private String encode(String num, int x)
{
    char[] charnum = args[1];
    for (x = 0; x <= charnum.length; x++)
    {
        charnum = charnum + x;
    }
    return new String(charnum);
}

What do I do? Similarly i have to write a decoder method. I was going to have the same set up except the effects of the for loop change to

charnum = charnum - x;

My problem is that when I try to compile, I get the following errors

symbol  : variable argslocation: class Lab041
    char[] charnum = args[1];
                     ^

Lab041.java:17: operator + cannot be applied to char[],int

        charnum = charnum + x;
                          ^

2 errors

how do i fix it? and is my assumption for the decode method correct?

WeekzGod
  • 313
  • 1
  • 8
  • 17

4 Answers4

2

Charnum is a character array. You want to do:

for (i = 0; i <= charnum.length; i++) {
    charnum[i] = (char) (charnum[i] + x);
}

Which will refer to an element of that array.

Also keep in mind that you don't have safe guards for if you go over 'z'

Also check out this: Simple caesar cipher in java

Community
  • 1
  • 1
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
2

I would start with a simpler encode method, like this

private static String encode(String num, int x)
{
  StringBuilder sb = new StringBuilder();
  for (char c : num.toCharArray()) {
    sb.append((char) (c + x));
  }
  return sb.toString();
}

Then to use it, I'd use a main method like this

public static void main(String[] args) 
{
  if (args == null || args.length < 2) {
    System.out.println("Not enough arguments.");
    System.exit(1);
  }
  int val = Integer.valueOf(args[args.length - 1]);
  for (int i = 0; i < args.length - 1; i++) {
    if (i != 0) {
      System.out.print(' ');
    }
    System.out.print(encode(args[i], val));
  }
  System.out.println();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

java cmd line args is a string array, you can't simply assign it to a char array, instead String class has a method toCharArray()

Also make use of the fact char data type can be manipulated by addition operator, eg:

char c = 'a';
c = (char)(c + 1);  // c is now 'b'
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • the toCharArray technique worked but I don't follow the second bit of advice. how exactly do I implement that? – WeekzGod Nov 26 '13 at 01:35
1

class CaesarCipher {

 private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

 public String encrypt(String plainText,int shiftKey)

 {

       plainText = plainText.toLowerCase();

       String cipherText="";

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

       {

            int charPosition = ALPHABET.indexOf(plainText.charAt(i));

            int keyVal = (shiftKey+charPosition)%26;

            char replaceVal = this.ALPHABET.charAt(keyVal);

            cipherText += replaceVal;

       }

       return cipherText;

 }

 public String decrypt(String cipherText, int shiftKey)

 {

       cipherText = cipherText.toLowerCase();

       String plainText="";

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

       {

            int charPosition = this.ALPHABET.indexOf(cipherText.charAt(i));

            int keyVal = (charPosition-shiftKey)%26;

            if(keyVal<0)

            {

                  keyVal = this.ALPHABET.length() + keyVal;

            }

            char replaceVal = this.ALPHABET.charAt(keyVal);

            plainText += replaceVal;

       }

       return plainText;

 }

}

pavithra
  • 11
  • 1