0

I am making a substitution cipher using the periodic table and I've run into some issues while trying to define some variable with array indexes.

I've tried using some conditional statements to bypass the issue but it always seems to ruin the logic and make the application crash.

public class ENcryptionProcedures2 {
    public static  String strOriginal;
    public static String[] arrofStr;
    public static boolean val;
    public static int newval;

    private static final String[] arrPeriodicTableValues = {"h", "he", "li", "be", "b", "c", "n", "o", "f", "ne", "na", "mg", "al", "si", "p", "s", 
        "cl", "ar", "k", "ca", "sc", "ti", "v", "cr", "mn", "fe", "co", "ni", "cu", "zn", "ga", "ge", "as", "se", "br", "kr", "rb", "sr", "y", "zr", "nb", "mo", 
        "tc", "ru", "rh", "pd", "ag", "cd", "in", "sn", "sb", "te", "i", "xe", "cs", "ba", "la", "ce", "pr", "nd", "pm", "sm", "eu", "gd", "tb", "dy", "ho", "er", "tm", "yb",
        "lu", "hf", "ta", "w", "re", "os", "ir", "pt", "au", "hg", "tl", "pb", "bi", "po", "at", "rn", "fr", "ra", "ac", "th",
        "pa", "u", "np", "pu", "am", "cm", "bk", "cf", "es", "fm", "md", "no", "lr", "rf", "db", "sg", "bh", "hs", "mt", "ds", "rg", "cn", "nh", "fl", "mc", "lv", "ts", "og"};

    public static String sc(){
        Scanner sc = new Scanner(System.in);
        String strOriginal = sc.nextLine();
        return strOriginal;
    }

    public static int scI(){
        Scanner sc = new Scanner(System.in);
        int sh = sc.nextInt();
        return sh;
    }

    private static int checkingfunc(String letters){
        int num = 0;
        for(int f=0; f<=117; f++){
            if(arrPeriodicTableValues[f].equalsIgnoreCase(letters)){
               num= f+1;
               return num;
            }  else{
             num = 0;
            }
        }
        return 0;
    }

    public static String Periodicencrypt(String strOriginal, String[] arrofStr){

    String encryptedString=""; 

    arrofStr = strOriginal.split("");

    int iLen= strOriginal.length();
    for(int i=0; i<=iLen; i++ ){

        String oneletter = arrofStr[i];
        String twoletters = arrofStr[i]+arrofStr[i+1];
        System.out.println(twoletters);

        if(checkingfunc(twoletters)!=0){
           encryptedString= encryptedString + Integer.toString(checkingfunc(twoletters));

        } else if(checkingfunc(oneletter)!=0){
           encryptedString=encryptedString + Integer.toString(checkingfunc(oneletter));
        } else{
           encryptedString=encryptedString +oneletter;
        }
    }
    return encryptedString;
}
3
  • It means you're trying to assign a a value to array[1], when there is no such index. The exception tells you the line where you're trying to do that. Commented Nov 3, 2019 at 19:19
  • Don't hardcode the size of your array (f<=117). Let the array give that to your code: for(int f=0; f < arrPeriodicTableValues.length; f++){. Also, use only one Scanner object. You don't need more than one, declare it as a class member variable along with your other member variables. Commented Nov 3, 2019 at 19:25
  • 1
    Does this answer your question? Array Index Out of Bounds Exception (Java) Commented Nov 3, 2019 at 20:05

2 Answers 2

4

You are iterating array beyond its size, change

for(int i=0; i<=iLen; i++ ){

to

for(int i=0; i<iLen; i++ ){

Edit:

Another problem would be here

String twoletters = arrofStr[i]+arrofStr[i+1];

when your value i is at max (iLen-1), this will also cause an error, you need to fix your algorithm here

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you now i see it. I'm trying to fix it but I'm a bit stumped because i tried using an if statement to restrict it when i=iLen -1 but I can't figure out how the logic would work out since the looping starts being confusing. As for what the other user suggested, I don't know how i could use foreach with two characters since it only cycles character by character. Thank you.
@pleasehelp don't get confused. for-each can't be used in your case, as you need to access two index
but how do I fix it
it's your algorithm, I can suggest a quick fix by changing to this String twoletters = arrofStr[i]+arrofStr[((i+1) == iLen) ? 0: (i+1)], otherwise you will not be able to form two character string everytime
@pleasehelp were you able to fix your problem
0

In addition to what Shailesh Chandra wrote. You can also use foreach like that:

for(String st: someStringArray ){
    do something with "st"
}

In this method you will iterate over all strings in the array one by one and you don't have to worry about going out of bounds

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.