-3

I want to restart my for-loop. I have a homework problem, in which I have to tell the coordinates of user given word (one alphabet at a time) in a 2D array of A to Z.

for example : if the user gives a word "GREAT", then the program have to print the coordinates of each letter's location in a jagged array. (G - 1,1 , R - 3,2 , E - 2,1, A - 0,0 , T - 3,4)

Using nested for loops, I'm able to print forward characters (what I mean is, the word "GOT" have characters going from A to Z) and if I try "GET" (here, after 'G', 'E' is going backward), the program stops after 'G'. So, I guess if I could restart the loop after each letter's coordinates is printed, I could print all the coordinates.

`//2D array
char a[][] = {{'A','B','C','D','E'},{'F','G','H','I','J'},{'K','L','M','N','O'},{'P','Q','R','S','T'},{'U','V','W','X','Y'},{'Z'}}; 
//if the user given word is GET,
//then the output should be
//G-1,1
//E-0,5
//T-3,5`

3 Answers3

1

You don't need to restart a loop for this as it would be very inefficient to have an individual loop for each character in your word.

The way to solve this is to use a java map where the key is the character and the value is the entry in your initial array.

Just create a class call WordCoordinatesLocator (or whatever sounds good to you)) that will take your bi-dimensional array and build up the Map in the constructor. Save the map as an instance variable then expose a public method say getCoordinates(String word) that will access the map for each character and build up your response.

You may want to throw an exception if you get invalid characters: characters not included in the original alphabet array. Then create a unit test to prove it is working as expected.

Julian
  • 3,678
  • 7
  • 40
  • 72
0

There are several way to do this. A simple method is you can go for each char in the word and print its position. Why do you need to restart the loop if you check for each char. You can use java maps as said above or even hashtable but this is not a big task.
A simple code that can help you.

        String word = "GET";
        char a[][] = {{'A','B','C','D','E',' '},{'F','G','H','I','J',' '},{'K','L','M','N','O',' '},{'P','Q','R','S','T',' '},{'U','V','W','X','Y',' '},{'Z',' ',' ',' ',' ',' '}};
        int l=0;
        while(l<word.length()) {
            for (int i = 0 ; i < 6; i++)
                for(int j = 0 ; j < 6 ; j++)
                    if (word.charAt(l) == a[i][j])
                        System.out.println(i + " " + j);
            l++;
        }

You can even use switch or multiple if else lol...

Santhosh Arun
  • 106
  • 1
  • 7
0

Since The last row is special,it contain only one char Z, so we remove from the two-dimensional array,then the array will be a 5 * 5 array.

public static void print(char[][] array, String input) {
        if (null == input || input.isEmpty()) {
            return;
        }
        for (int i = 0; i < input.length(); i++) {
            char var0 = input.charAt(i);
            // Z is special we check first
            if (var0 == 'Z') {
                System.out.println("Z-6,1");
                continue;
            }
            // Traversing the array to find coordinate
            for (int row = 0; row < 5; row++) {
                for (int clo = 0; clo < 5; clo++) {
                    char var1 = array[row][clo];
                    if (var0 == var1) {
                        System.out.println(var0 + "-" + row + "," + clo);
                        break;
                    }
                }
            }
        }
    }
TongChen
  • 1,414
  • 1
  • 11
  • 21