简体   繁体   中英

where (and what) is the error - java cipher

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.

You are doing

mynamechars[i]

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

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 27 th 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];
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM