When Im trying to take a String and throw it into an array by putting the build method inside the print method but when I do I get the following errors:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at polycipher.CipherKey.printMatrix(CipherKey.java:27)
at polycipher.CipherKey.assembleMatrix(CipherKey.java:17)
at polycipher.ApplicationDriver.main(ApplicationDriver.java:23)
I was wondering what I'm doing wrong here? I'm pretty sure you can call a method within a method, is there something wrong with my codes logic? I tryed adding -1 to pretty much everywhere that I thought could cause an array out of bounds but no luck.
import java.util.*;
import polycipher.Matrix;
public class CipherKey {
Scanner write = new Scanner (System.in);
Matrix polyCipher = new Matrix();
private final char[] PHRASE_KEY = { 'J', 'A', 'V', 'A'};
String[][] matrixStore = new String[PHRASE_KEY.length][PHRASE_KEY.length];
public void assembleMatrix() {
matrixFormatter(polyCipher.translation);
System.out.println(polyCipher.translation);
printMatrix(buildeMatrix(polyCipher.translation,"|",","));
}
public String printMatrix(String s [][]){
String keyOut = " J A V A\n";
for (int i = 0; i < PHRASE_KEY.length; i++) {
keyOut += PHRASE_KEY[i] + " ";
for (int j = 0; j < PHRASE_KEY.length; j++) {
keyOut += s[i][j] + " ";
}
keyOut += "\n";
}
return keyOut;
}
public static String [][] buildeMatrix (String translation, String outermarker, String innermarker) {
// outerdelim may be a group of characters
String [] sOuter = translation.split ("[" + outermarker + "]");
int size = sOuter.length;
// one dimension of the array has to be known on declaration:
String [][] result = new String [size][];
int count = 0;
for (String line : sOuter)
{
result [count] = line.split (innermarker);
++count;
}
return result;
}
public String matrixFormatter(String x){
String resultstr = "";
int i = 0;
while(i < x.length()) {
// If end of string: only add character.
if (i == x.length() - 1) {
resultstr += x.substring(i, i + 1);
} else {
if ( ((i + 1) % 4) == 0) {
resultstr += x.substring(i, i + 1) + "|";
} else {
resultstr += x.substring(i, i + 1) + ",";
}
}
i++;
}
return resultstr;
}
}
This is how I call the matrix in my main
CipherKey Key = new CipherKey();
Key.assembleMatrix();
Aucun commentaire:
Enregistrer un commentaire