1

I'm trying to write a script, that ciphers a plain text and got it so far, that it could work. I have a variable offset that has the range a letter increments. A method encrypt goes through the string as long as it is and increments all uppercase letters as much as the offset says. All fine so far. But I struggle with passing the values for both to the method encrypt. I can easily pass the int to the contructor with creating an instance but this doesn't work for the method.

public class Caesar {

    int offset;

    public int kryptograph(int offset){
        return this.offset=offset;
    }

    public String encrypt(String klartext) {
        int wl = klartext.length()-1;
        String text = null;

        for(int i = wl; i >= 0; i++){
            if (Character.isUpperCase(klartext.charAt(i))){
                text += klartext.charAt(i)+kryptograph(offset);
            }
            else {
                text += klartext.charAt(i);
            }
        }
        return text;
    }

    /*public String decrypt(String text2) {
        ;
    }*/

    public static void main(String[] args) {

        Caesar o = new Caesar();
        o.kryptograph(7);
        Caesar k = new Caesar();
        k.encrypt("TEST");

    }

}

I looked up some other tutorials but couldn't find a solution that looked like mine. So not much to compare there.

Edit:

This is the Exception I get:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    at java.lang.String.charAt(String.java:658)
    at javaapplication25.Caesar.encrypt(Caesar.java:16)
    at javaapplication25.Caesar.main(Caesar.java:35)
C:\Users\Kendel\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
8
  • 1
    What is your problem? What happens when you try to compile/run/... the code? Commented Dec 11, 2016 at 19:49
  • 2
    k.encrypt("TEST"); is correct.... What is the problem, exactly? Commented Dec 11, 2016 at 19:49
  • I don't understand the question. Commented Dec 11, 2016 at 19:50
  • You don't have an explicit constructor Commented Dec 11, 2016 at 19:50
  • 1
    You are looping the wrong way. i--, not i++... Commented Dec 11, 2016 at 19:51

1 Answer 1

3

You exception source - you are starting to iterate from n-1 element, but then you increment the i variable so you are trying to receive character at position n, which leads to this exception.

Try replacing your code with this:

public class Caesar {

    private final int offset;

    public Ceasar(int offset){
        this.offset=offset;
    }

    public String encrypt(String klartext) {
     StringBuilder text = new StringBuilder();

     for(int i = klartext.length()-1; i >=0; i--){
        char value = klartext.charAt(i);
        if (Character.isUpperCase(value)){
            text.append(value + kryptograph(offset));
        } else {
            text.append(value);
        }
     }
     return text.toString();
    }

    /*public String decrypt(String text2) {
       // to  be implemented
    }*/

    public static void main(String[] args) {
        Caesar o = new Caesar(7);
        o.encrypt("TEST");
    }

}

Offset should be final and bound to Ceasar instance, so that each Ceasar class instance will always code and decone in same way.

Sign up to request clarification or add additional context in comments.

1 Comment

And fix the loop increment (or rather, decrement)

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.