0

i am making a cipher, and for some reason it gives me these errors after i input the text:

enter string to be encrypted: 
hello world
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
    at chipher.cipher.encrypt(cipher.java:21)
    at chipher.cipher.main(cipher.java:9)

this is my code:

package chipher;
import java.util.Scanner;
public class cipher {
public static int x;
public static int y;
public static Scanner jon = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("enter string to be encrypted: ");
        encrypt(jon.nextLine());
        }


public static void encrypt(String tocipher){
    double lngth = tocipher.length();
    tocipher.toLowerCase();
    char[] mynamechars = tocipher.toCharArray();
    char[] alphabet = new char[]{'a', 'b', 'c', 'd' , 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for(int i = 0; i<lngth;)
    for(int x = 0; x<26;){
        y = x + 1;
if(mynamechars[i] == alphabet[x]){
    mynamechars[i] = alphabet[y];
}
i++;
x++;
}
    String text = String.valueOf(mynamechars);
    System.out.println(text);
}
}

i don't know what is going on and i am just learning java so it may be something basic, so just bear with me here.

  • and yes i know it is a very basic cipher, but just try to help please – user2224340 Jul 18 '13 at 23:56
  • Can you format your code better? – Daniel Kaplan Jul 18 '13 at 23:58
  • `tocipher.toLowerCase();` has no effect. – SLaks Jul 19 '13 at 00:00
  • 2
    @user2224340 I suggest you get acquainted with your IDE's debugger as it can be very useful, now, and in the future. – nanofarad Jul 19 '13 at 00:03
  • I know you are just playing around now but if you ever have to, don't bother creating your own "security encryption", just use one of the built in cryptography `APIs` such as `javax.crypto.Cipher`. See [this question](http://stackoverflow.com/questions/1132567/encrypt-password-in-configuration-files-java). – 0x6C38 Jul 19 '13 at 00:07
  • 1
    What part of the error message do you not understand? You are trying to access the 12th element of an array that has fewer than 12 elements. On the line it tells you. – Brian Roach Jul 19 '13 at 00:09

3 Answers3

0

You are doing

mynamechars[i]

upto 25 th index and it seems the input you entered is just 10 character long

jmj
  • 237,923
  • 42
  • 401
  • 438
0

You've got two errors. Your first one is due to the way you wrote the for loops and your lack of indentation:

for (int i = 0; i < lngth;) {
    for (int x = 0; x < 26;) {
        y = x + 1;

        if (mynamechars[i] == alphabet[x]) {
            mynamechars[i] = alphabet[y];
        }

        i++;
        x++;
    }

    String text = String.valueOf(mynamechars);
    System.out.println(text);
}

You increment i in the inner loop along with x. You should be incrementing it outside of that loop:

for (int i = 0; i < lngth; i++) {
    for (int x = 0; x < 26; x++) {
        y = x + 1;

        if (mynamechars[i] == alphabet[x]) {
            mynamechars[i] = alphabet[y];
        }
    }

    String text = String.valueOf(mynamechars);
    System.out.println(text);
}

Then there's the problem with y.

x ranges from 0 to 25, so y will range from 1 to 26. alphabet doesn't have an element with an index of 26 (that's the 27th letter), which causes your error.

You either have to check for that case manually:

if (mynamechars[i] == alphabet[x]) {
    if (y == 26) {
        y = 0;
    }

    mynamechars[i] = alphabet[y];
}

Or get rid of y completely and use the modulo operator to wrap around to the beginning:

if (mynamechars[i] == alphabet[x]) {
    mynamechars[i] = alphabet[(x + 1) % alphabet.length];
}
Blender
  • 289,723
  • 53
  • 439
  • 496
0

Add a System.out.println above y = x + 1; and you should get the meaning of the exception :)

pyb
  • 4,813
  • 2
  • 27
  • 45