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)
k.encrypt("TEST");is correct.... What is the problem, exactly?i--, noti++...