-4

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.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • Also use System.print instead of println to get everything on one line. – avk Apr 25 '15 at 07:56
  • possible duplicate of [What is a debugger and how can it help me diagnose problems](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Apr 25 '15 at 08:21

1 Answers1

0

Your code sample missing essential information. However, the internal loop:

while(random)
{
   // . . .
}

can run forever because you don't guarantee to have random variable to be set to false. It will work only if you call the method with a word that isn't part of the array. Also if you call it with word = "soto" then a chance is 100% that never leave the loop since out of 4 words there is one that you found and other 3 are empty.

Leo Y
  • 659
  • 7
  • 22