So I have a method that receives a String[][] and a String(word).The method has to look for the word from the 2dArray and should display one of the other words in the same row. Also I need to ignore(not display) and count(++) the empty spots that are in the same row. I want to know why my counter is looping and it wont let me ignore those empty spaces.
public static void main(String[] args) {
String[][] array2d = {{"joe", "slim", "ed", "george"},
{"soto", "", "", "" },
{"billy", "sanchez", "carlos", "fernando"}};
sort(array2d, "soto");
}
public static void sort(String[][] matrix, String word) {
int counterForArrayLength = 0;
boolean random = true;
boolean exit = true;
String optionFromUser = "";
do {
random = true;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j].equals(word)) {
for (int k = 0; k < matrix[0].length; k++) {
while (random) {
String randomWord = matrix[i][(int) (Math.random() * matrix[0].length)];
String testRandom = "" + randomWord;
if (randomWord.equals(word)) {
random = true;
} else {
if (randomWord.equals("")) {
counterForArrayLength++;
System.out.println("" + counterForArrayLength);
} else {
JOptionPane.showMessageDialog(null, randomWord);
optionFromUser = JOptionPane.showInputDialog("Desea obtener otro sinonimo? si/no \n Digite salir si asi lo desea.");
optionFromUser = optionFromUser.toLowerCase();
if (optionFromUser.equals("si") || optionFromUser.equals("s")) {
random = true;
} else {
random = false;
}
}
}
}
}
}
}
}
if (optionFromUser.equals("salir")) {
exit = false;
} else {
word = JOptionPane.showInputDialog("Digite otra palabra");
}
} while (exit);
}
run:
1
2
3
4
5
6
7
8
9
10
etc.