-1

I have been trying to find my own way of making a Rot13 algorithm in java, but when I try a phrase, it gives me this error:

java.lang.ArrayIndexOutOfBoundsException: 41

So this is my code:

:Update with whole names,

public class Rot13
{
    char[] translated;
    String abc = "abcdefghijklmnopjrstuvwxyzabcdefghijklmnopqrstuvwxyz";

    public String ROT13(String input){
        input = input.toLowerCase();
        char[] sentence = input.toCharArray();
        char[] ABC = abc.toCharArray();
        int x = input.length();
        int y = 0;
        char[] translated = new char[x];
        for(int i = 0; i<x;i++){
            int z = 0;
            if(sentence[i] == ' '){
                translated[i] = ' ';
            }
            else {
                while(y==0){
                    if (sentence[i] == ABC[z]){
                        y =1;
                    }
                    else{
                        z += 1;
                    }
                }
                translated[i] = ABC[z+12];
            }
        }
        String rot13string = new String(translated);
        return rot13string;
    }
}

: Update 2 I just tested this version again and it translates the it. But in the wrong way, for example, "Hello" becomes "tmmmm" The first letter seems to be right but then the next ones are always 'm'.

Update 3: Thanks for your answers guys, here is my final code, I just duplicated my alphabet a "few" times. :

public class ROT13
{
     char[] translated; 
    String ab = "abcdefghijklmnopjrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    String abc = String.format("%0" + 1000 + "d", 0).replace("0",ab);
    public String ROT13(String input){ 
        input = input.toLowerCase();    
        char[] sentence = input.toCharArray();
        char[] ABC = abc.toCharArray();
        int length = input.length();
        char[] translated = new char[length];
        for(int i = 0; i<length;i++){
            int y = 0;
            int h = 0;
            if(sentence[i] == ' '){
                translated[i] = ' ';
            }
            else {
                while(y==0){
                    int z = 0;
                    if (sentence[i] == ABC[h]){
                        y +=1;
                    }
                    else{
                        z += 1;
                        h += 1;
                    }
                }
                translated[i] = ABC[h+13];
            }
        }
        String rot13string = new String(translated);
        return rot13string;
    }
}
  • 1
    I have no idea about java, but in another programming language, the error would be when `sentence` is shorter than `s`, as you are doing `sentence[i]` – Ander Biguri Apr 30 '18 at 16:55
  • Start with giving your variables decent names to make your code readable. – MrSmith42 Apr 30 '18 at 17:09
  • Also it's `opqrst` instead `opjrst` I believe. – Tamas Hegedus Apr 30 '18 at 17:09
  • I have rolled back your last edit. Questions are not intended for answers, and if - because it is closed - you want to publish your solution, then you should **add** it, instead of removing the original question. Removal of the question invalidated the [answer by JavaScriptCoder](https://stackoverflow.com/a/50105084/466862). – Mark Rotteveel Apr 30 '18 at 18:37

1 Answers1

1

Currently, think about what happens when there is a 'z' in the sentence. Then, you make     t[i] = ABC[z (26) + 12] //which is larger than ABC's length.

Personally, I would do rot13 like so:

public char rot13(char s){
    if (c >= 'a' && c <= 'm') return c += 13;
    else if  (c >= 'A' && c <= 'M') return c += 13;
    else if  (c >= 'n' && c <= 'z') return c -= 13;
    else if  (c >= 'N' && c <= 'Z') return c -= 13;
    else return c;
}