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;
}
array[1], when there is no such index. The exception tells you the line where you're trying to do that.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.