I need to create this class, but I can't get it to work:
public void printTable(): prints table (two-dimensional array). (optional: print the table with the two words in their correct positions in the first row and first column respectively.)
public class Levenshtein {
private String word1;
private String word2;
private int[][] table;
public Levenshtein(String aWord1, String aWord2) {
word1 = aWord1;
word2 = aWord2;
Levenshtein.table(word1, word2);
}
private static int[][] table(String word1, String word2) {
int[][] distTable = new int[word1.length()+1][word2.length()+1];
for (int i = 0; i < distTable.length; i++) {
distTable[i][0] = i;
}
for (int j = 0; j < distTable[0].length; j++) {
distTable[0][j] = j;
}
return distTable;
}
private static int min(int i1, int i2, int i3) {
return Math.min(Math.min(i1, i2), i3);
}
public int distance() {
int[][] distance = new int[word1.length() + 1][word2.length() + 1];
for (int i = 0; i <= word1.length(); i++)
distance[i][0] = i;
for (int j = 1; j <= word2.length(); j++)
distance[0][j] = j;
for (int i = 1; i <= word1.length(); i++)
for (int j = 1; j <= word2.length(); j++)
distance[i][j] = min(
distance[i - 1][j] + 1,
distance[i][j - 1] + 1,
distance[i - 1][j - 1] + ((word1.charAt(i - 1) == word2.charAt(j - 1)) ? 0 : 1));
return distance[word1.length()][word2.length()];
}
public void printTable() {
String tableArray[][] = new String[word1.length()+2][word2.length()+2];
//Header Row
for (int i = 0; i < word1.length()+2; i++) {
if (i == 0 || i == 1) {
tableArray[i][0] = "*";
} else {
String tmp = String.valueOf((word1.charAt(i-2)));
tableArray[i][0] = tmp;
}
}
//Header Column
for (int j = 0; j < word2.length()+2; j++) {
if (j == 0 || j == 1) {
tableArray[0][j] = "*";
} else {
String tmp = String.valueOf((word2.charAt(j-2)));
tableArray[0][j] = tmp;
}
}
//Initialize column 0 (column 0 = header, start column = 1)
for (int k = 1; k < tableArray.length-2; k++) {
int tmp = k;
tableArray[1][k] = String.valueOf(tmp-1);
}
for (int l = 1; l < tableArray.length-1; l++) {
int tmp = l;
tableArray[l][1] = String.valueOf(tmp-1);
}
System.out.println(tableArray[1][1]);
//Filling Table
for (int i = 2; i <= word1.length(); i++) {
for (int j = 2; j <= word2.length(); j++) {
tableArray[i][j] = String.valueOf(min(
Integer.valueOf(tableArray[i - 1][j]) + 1,
Integer.valueOf(tableArray[i][j - 1]) + 1,
Integer.valueOf(tableArray[i - 1][j - 1]) + ((word1.charAt(i - 1) == word2.charAt(j - 1)) ? 0 : 1)));
}}
//Print Table
for (int m = 0; m < word1.length()+2; m++) {
//System.out.print(tableArray[m][0]);
for (int n = 0; n < word2.length()+2; n++) {
System.out.print(tableArray[m][n] + " | ");
}
if (m < word1.length()+2 ) { System.out.print("\n"); }
}
}
}
MainClass:
public class MainClass {
public static void main(String[] args) {
Levenshtein lev = new Levenshtein("face", "ape");
Levenshtein lev2 = new Levenshtein("ape", "face");
//System.out.println(lev.distance());
lev.printTable();
lev2.printTable();
}
}
This will output:
0
* | * | a | p | e |
* | 0 | 1 | 2 | null |
f | 1 | 1 | 2 | null |
a | 2 | 2 | 2 | null |
c | 3 | 3 | 2 | null |
e | null | null | null | null |
0
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.valueOf(Integer.java:766)
at Levenshtein.printTable(Levenshtein.java:90)
at MainClass.main(MainClass.java:11)
So in the first one it doesn't calculate the last column, and in the 2nd it just doesn't work.