java - Array throwing nulls instead of Strings -
this array gets parameter bunch of words stored in array.
if array has 6 spaces has 4 words in it, new array must downsize 4 spaces, put example. if array has 6 spaces , no words in it, array must filled 4 spaces string "not found".
the problem i'm having array throwing nulls instead of putting "not found".
what can problem?
public static void getallsynonyms(string[][] matrix, string wordtobefound) { int arraylenght = 0; string synonyms[] = new string[matrix[0].length]; (int = 0; < matrix.length; i++) { (int j = 0; j < matrix[0].length; j++) { if (matrix[i][j].equals(wordtobefound)) { (int k = 0; k < matrix[0].length; k++) { if (!matrix[i][k].equals("")) { synonyms[i] = matrix[i][k]; } else { arraylenght = matrix[0].length - 1; synonyms[i] = "not found"; int counter = 0; counter++; } system.out.println("" + synonyms[i]); } } } } string synonymsarray[] = new string[arraylenght]; (int = 0; <synonyms.length; i++) { system.out.println(""+ synonyms[i]); if (synonyms[i] != "not found") { synonymsarray[counter2++] = synonyms[i]; } } }
the nulls appear in second system.out.
this matrix.
string[][] matrix = {{"joe", "slim", "ed", "george"}, {"soto", "", "asdrubal", ""}, {"billy", "sanchez", "carlos", "fernando"}};
matrix.length =3, matrix[0].length =4
as iterate in outer loop until matrix.length, initialized synonyms string[matrix[0].length], synonyms[3] never reached , therefor null
edit: in rows of matrix searchword can't found , never set synonyms[i]. set in block
if (matrix[i][j].equals(wordtobefound)) {
but if false, synonyms array never modified
Comments
Post a Comment