Alex
Alex

Reputation: 55

Encoding string using cipher table

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.

Upvotes: 0

Views: 164

Answers (1)

Jason Braucht
Jason Braucht

Reputation: 2368

This line is the likely culprit.

int column = Character.getNumericValue(Integer.parseInt(m[p]));

The NumberFormatException is from the Integer.parseInt(). If you inspect m[p] you'll find that it is an array of length 1 with an where m[0]="Happy Birthday". The String "Happy Birthday" can't be parsed as a number, thus the exception.

If you want to break a String into its individual characters, you could use String.toCharArray() like this: char[] m = message.toCharArray();

There are some other issues in your code:

  • In the main() method an instance of Prog3Cipher is being created, but encode() is static, so the call to self.encode() likely isn't working as you might expect. Removing static from the method declaration of encode() should fix that problem.
  • Character.getNumericValue() returns the unicode value of the provided character. That method returns 10 for 'A' through 35 for 'Z', so the values don't match the bounds of your cipherTable array.
  • As written, the for loop in encode() will walk past the end of the keyList[] array. This is because keyList[] only has 7 characters compared to the 14 chars in message.
  • The encode() method doesn't have any special handling for spaces in message, so they'll most likely also cause an exception.

Upvotes: 1

Related Questions