I need to encode a given message using cipherTable and a given key. I'm pretty sure there's something wrong with the encode method but i'm not sure what. This code:
import java.util.ArrayList;
public class Prog3Cipher {
// INSTANCE VARIABLES
static char[] keyList; // VARIABLE DESCRIPTION COMMENT
static char[][] cipherTable; // VARIABLE DESCRIPTION COMMENT
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String encode(String message) {
String result = "";
ArrayList<Character> w = new ArrayList<>();
String[] m = new String[]{message};
for (int p = 0; p < m.length; p++) {
int row = Character.getNumericValue(keyList[p]);
int column = Character.getNumericValue(Integer.parseInt(m[p]));
char q = cipherTable[row][column];
w.add(q);
}
result = String.join(" ", (CharSequence) w);
return result;
}
public Prog3Cipher(char code, String key) {
keyList = key.toCharArray();
int offset = code - 'A';
cipherTable = new char[26][26];
for (int x = 0; x < cipherTable.length; x++) {
for (int y = 0; y < cipherTable[0].length; y++) {
cipherTable[x][y] =
alpha.charAt((x + y + offset) % alpha.length());
}
}
}
public static void main(String[] args) {
// Testing only works if using VM argument -ea
Prog3Cipher self = new Prog3Cipher('H', "BABBAGE");
assert "PHXXF MQYBPKNJ".equals(self.encode("Happy Birthday"));
}
}
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Happy Birthday"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Prog3Cipher.encode(Prog3Cipher.java:24)
at Prog3Cipher.main(Prog3Cipher.java:68)
Process finished with exit code 1
the encode method takes a cipher table, cipherTable, and finds the intersection point of the row and column, which are found with keyList and m, and the intersection is set to q and then added to an Arraylist w. Result is set to a String and returned.
i'm not sure what's wrong as there are no errors with the code so if anyone could look over it and give some guidance it would be much appreciated.