Hi Guys, wonder if any one can help??

I have been asigned some home work to make a CaesarCipher class, listed below, where by there is a test class below which would need to be passed to it. I have figured out the first bit but cant debugg and get it to work. Hope you can help

Tom Keys

//First Class file

public class CaesarTest {
    public static void main( String[] args ) {
        CaesarCipher c = new CaesarCipher( 10 );
        String output;
        output = c.encrypt( "The quick brown fox jumps over the lazy dog" );
        System.out.println( output );
        output = c.decrypt( output );
        System.out.println( output );
    }
}





//Second Class File


public class CaesarCipher {

    private int key;


    public CaesarCipher( int inKey ) {
        key = inKey;
    }

    public String encrypt( String s ) {
       int lengthOfString = s.length();
       StringBuffer encrypted = new StringBuffer();
       for (int i=0; i< lengthOfString; i++) {
        char tempChar = s.charAt(i);
        encrypted.append( encrypt( tempChar ) );

       }
           return i;

        }

    //public String decrypt( String s ) {
        // Decrypt the argument a character at a time & return the result
    //}



    private char encrypt( char c ) {
        c = charact;

        //Encrypt the argument & return the result


      //char charact = stringName.getCharAt(i);
      charact = charact + key;
      outString.append(charact);



    }




    //private char decrypt( char c ) {
        // Decrypt the argument & return the result
    //}

}

Dani AI

Generated

The posted CaesarCipher attempt has a few clear compile and logic problems: wrong return types, undefined locals, and incorrect per-character handling. is right to mention character ranges — a classic Caesar only shifts letters (A–Z, a–z) and leaves spaces/punctuation as-is. Below is a concise, correct approach and a minimal working implementation that normalizes the key, preserves case, and leaves non-letters untouched.

Key points to follow:

  • Normalize the key into 0..25: ((key % 26) + 26) % 26.
  • Build the result with StringBuilder.
  • For each char, if it’s a..z or A..Z rotate inside that range using modulo; otherwise append unchanged.
  • encrypt and decrypt are symmetric: decrypt can call encrypt with the inverse key.
  • Watch char-to-int promotion and cast back to char.

A working implementation:

public class CaesarCipher {
    private final int key;

    public CaesarCipher(int key) {
        this.key = ((key % 26) + 26) % 26;
    }

    public String encrypt(String input) {
        StringBuilder sb = new StringBuilder(input.length());
        for (char ch : input.toCharArray()) {
            sb.append(shift(ch, key));
        }
        return sb.toString();
    }

    public String decrypt(String input) {
        return new CaesarCipher((26 - key) % 26).encrypt(input);
    }

    private char shift(char ch, int k) {
        if (ch >= 'a' && ch <= 'z') {
            return (char) ('a' + (ch - 'a' + k) % 26);
        } else if (ch >= 'A' && ch <= 'Z') {
            return (char) ('A' + (ch - 'A' + k) % 26);
        } else {
            return ch;
        }
    }
}

Troubleshooting hints: “cannot find symbol” means a variable or method name is misspelled or out of scope; mismatched return types cause compile errors; casting is needed when converting int results back to char. For background on the cipher see Caesar cipher. For Java helpers (e.g., StringBuilder, Math.floorMod) consult the Java docs (for example, the Math page at https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html).

What's the problem? I assume it's with the output? You need to check the ascii ranges for viewable characters. There's no need to encrypt new line characters and those out of the viewable range.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.