Kendel Ventonda
Kendel Ventonda

Reputation: 411

Passing a string to a method in Java

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)

Upvotes: 1

Views: 222

Answers (1)

Beri
Beri

Reputation: 11600

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.

Upvotes: 3

Related Questions