1

I have 4x4 character array here and I need to get the common value of the character that is on the edge of the array...I tried the solutions from other questions similar to my problem, but I am still getting the same error.,

here's my code..

//arr2[][]
//      arr2[3][0] = 'H';
//      arr2[3][1] = 'E';
//      arr2[3][2] = 'L';
//      arr2[3][3] = 'P';
//arr3[][]
//      arr3[1][3] = 'T';
//      arr3[2][3] = 'O';
//      arr3[3][3] = 'P';
//I specifically need the get the 'P' at [3][3]..
for(o = 0;o<count;o++){
        char letter = out.charAt(o);                        
        for(int m = 0; m < 4; m ++){    
            for(int n = 0; n < 4; n ++){
                if(Arrays.asList(arr3[m][n]).contains(letter)){ 
                    r = m;
                    c = n;
                }
            }
        }
        right  = arr2[r][c+1];
        left  = arr2[r][c-1];
        up  = arr2[r-1][c];
        down  = arr2[r+1][c];
        if(o==0){
                if(c==0){
                    if(r==0||r==3){
                        if(right!=null){
                            l = right;
                        }
                    }else{
                        if(right!=null){
                            l = right;
                        }else if(up!=null){
                            l = up;
                        }
                    }
                }else if(c==3){
                    if(r==0||r==3){
                        if(left!=null){
                            l = left;
                        }
                    }else{
                        if(left!=null){
                            l = left;
                        }else if(up!=null){
                            l = up;
                        }
                    }
                }else{
                    if(r==0||r==3){
                        if(left!=null){
                            l = left;
                        }else if(right!=null){
                            l = right;
                        }
                    }else{
                        if(left!=null){
                            l = left;
                        }else if(right!=null){
                            l = right;
                        }else if(up!=null){
                            l = up;
                        }
                    }
                }
            }
        }else if(o==(count-1)){
            if(vertical == 1){
                if(c==0){
                    if(r==0||r==3){
                        if(right!=null){
                            l = right;
                        }
                    }else{
                        if(right!=null){
                            l = right;
                        }else if(down!=null){
                            l = down;
                        }
                    }
                }else if(c==3){
                    if(r==0||r==3){
                        if(left!=null){
                            l = left;
                        }
                    }else{
                        if(left!=null){
                            l = left;
                        }else if(down!=null){
                            l = down;
                        }
                    }
                }else{
                    if(r==0||r==3){
                        if(left!=null){
                            l = left;
                        }else if(right!=null){
                            l = right;
                        }
                    }else{
                        if(left!=null){
                            l = left;
                        }else if(right!=null){
                            l = right;
                        }else if(down!=null){
                            l = down;
                        }
                    }
                }
            }
        }else{
            if(vertical == 1){
                if(c==0){
                    if(right!=null){
                        l = right;
                    }
                }else if(c==3){                                 
                    if(left!=null){
                        l = left;
                    }
                }else{
                    if(right!=null){
                        l = right;
                    }else if(left!=null){
                        l = left;
                    }
                }
            }
        }
        k = Character.toString(letter);
        letr = Character.toString(l);
kathy
  • 27
  • 1
  • 6
  • Why do you write `if(Arrays.asList(arr3[m][n]).contains(letter)){` instead of `if (arr3[m][n] == letter){` ?? – jlordo Feb 21 '13 at 14:45
  • @jlordo I have other programs above this one, I created a string from the values of arr3 which is TOP..and I want to know the location of each character and the way to do that is to check if it is contained in arr3[m][n]... – kathy Feb 21 '13 at 14:51
  • I guess you are getting no anwers, because your code is overly complicated to read. Many `if`, `else if` and `else` cases, with single letter variable names. You say `//I specifically need the get the 'P' at [3][3]..` this can be done by the single line `arr3[3][3]`. I don't understand your code, neither your question. As for my previous comment, both conditions are the same, whereas mine is easy to read and understand (and more efficient), whereas your's is overly complicated. – jlordo Feb 21 '13 at 14:56
  • You need the common values in the four edges of the array? For example, if both arrays have `[0][3] == 'r'`, it should be found too? – sp00m Feb 21 '13 at 15:13
  • @jlordo sorry if my code looks so complicated, but if you will just look at it closer, you will see that its just simple...i am trying different ways of doing it,i am just new in this language...can you teach me a easier way of doing this...my main goal is to get the common character with the same index in two different array... – kathy Feb 21 '13 at 15:15
  • @sp00m yes, ineed to get the common value of two different array at the same index..and in my predefined values of the two array, i muts be able to get 'P' which is on the edge of the array.. – kathy Feb 21 '13 at 15:19
  • @kathy But whatever the index, or only *edge* indexes? For example, if both arrays have `[1][2] == 'r'`, it should be found too? The first sentence of your post seems to say that it should not. – sp00m Feb 21 '13 at 15:37

3 Answers3

0

Can you run this and let us know if it helps?

char[][] arr2 = ... // your array
char[][] arr3 = ... // your other array
// inspect all common indeces:
for (int i = 0; i < arr2.length && i < arr3.length; i++) {
    for (int j = 0; j < arr2[i].length && j < arr3[i].length; j++) {
        // print value, if value is the same: 
        if (arr2[i][j] == arr3[i][j]) {
            // here you know that two characters on the same index are the same.
            // you have the information about the character and the indeces:
            System.out.println("Found common value at index (i,j)->"
                    + "(" + i + "," + j + "), char '" + arr2[i][j] + "'");
        }
    }
}

Using an array like the one in your commented example, this gives the following output:

Found common value at index (i,j)->(3,3), char 'P'

which seems to be what you are looking for. Assuming both arrays have the same dimension, this solution looks at every index in both arrays. If one array is bigger than the other, then only the common indices are being looked at. You mentioned something about the edges of your array. It is unclear to me, if you mean only the edges or including the edges. This answer includes the edges. If you only need the edges being looked at, feel free to leave a comment below the answer.

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • It seems to me that the OP wanted only common values that were on the *edges* of the arrays... But it still need confirmation from her. – sp00m Feb 21 '13 at 15:35
  • @sp00m: If OP confirms that I can easily adapt my answer ;) – jlordo Feb 21 '13 at 15:35
  • @kathy: Glad to help. Please don't forget to accept the most helpful answer. See how it works here: [How does accepting an answer work?](http://meta.stackexchange.com/a/5235/186652). Also upvote any other helpful answer as it helps all of us gain reputation. – jlordo Feb 21 '13 at 15:44
0

In your code,

for(int m = 0; m < 4; m ++){    
    for(int n = 0; n < 4; n ++){
        if(Arrays.asList(arr3[m][n]).contains(letter)){ 
            r = m;
            c = n;
        }
    }
}

Here you are assigning r and c which could probably be 0 or 3 at some case and then your code below,

right  = arr2[r][c+1];
left  = arr2[r][c-1];
up  = arr2[r-1][c];
down  = arr2[r+1][c];

In the above assignments, you are having c + 1, r + 1, c - 1, r - 1 which would surely be out of bounds if the previous loop assigns r and c with either 0 or 3.

Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
  • Madamsamy yes, that is why I have many if else statement to prevent going out of bounds., – kathy Feb 21 '13 at 15:45
  • The above assignments are even before those if statements. – Marimuthu Madasamy Feb 21 '13 at 15:46
  • so i need to put those statement inside the if statement?..i juts thought to put it there so that i do not need to repeatedly declare it..thanks for the help – kathy Feb 21 '13 at 15:48
  • No, you need to make sure that r and c are not at their bounds before you do those assignments. For example, ```right = c == 3 ? arr2[r][c] : arr2[r][c+1]``` which is effectively saying if c is at the bounds, do not increment but assign as it is, otherwise ```c + 1```. Similarly you need to this for r = 0 or c = 0. – Marimuthu Madasamy Feb 21 '13 at 15:51
  • but if i need to look for values surrounding the particular element...and if i will not increment, i will not be able to get the value – kathy Feb 21 '13 at 16:05
  • But if r and c are at their bounds, how can you get the surrounding elements? – Marimuthu Madasamy Feb 21 '13 at 16:10
  • as i have said, that is why i have all those if statements to prevent going out of bounds..but i get an error when run..i double check the logic and i think i did it right.. – kathy Feb 21 '13 at 16:16
  • you are checking for null in your if statements which is what you need to set in those assignments. What is the type of ```right```? – Marimuthu Madasamy Feb 21 '13 at 16:26
0

thank to those who helped me figure this one out.. I was able to get the values at the edge of an array using modulo..I was able to prevent going out of bounds... https://stackoverflow.com/a/12743834/2078012 this link from another question answered my problem.. Thank you again..:-)

Community
  • 1
  • 1
kathy
  • 27
  • 1
  • 6