Home > Mobile >  2D Array palindrome checker
2D Array palindrome checker

Time:11-25

We are tasked to find if a the 2D array is a palindrome row wise and column wise with a String or Character variable. How should I approach my code? we are just at the lesson of 2D arrays and loops at the moment and I am getting confused for some concept that I found on different forums because a lot of them are talking ab0out "MATRIX". Can someone explain to me how to check if the 2D array I inputted is a palindrome or not, row-wise and column-wise. `

public class Hard {
    public static boolean isLetterPal (String[][] array){
        int i = 0;
        int j = array.length - 1;
        while (i < j) {
            if (array.length != array.length) {
                return false;
            }
            i  ;
            j--;
        }
        return true;
    }

    public static void main(String[] args) {
        
        String[][] array = { { "A", "B", "A", "B", "A" },
                             { "B", "B", "A", "B", "A" },
                             { "A", "B", "A", "B", "B" },
                             { "B", "A", "B", "B", "A" } };
        System.out.println("The array is: ");
        for (int indRow = 0; indRow < array.length; indRow  ) {
            for (int indCol = 0; indCol < array[indRow].length; indCol  ) {
                System.out.print(array[indRow][indCol]   " ");
                if (indCol == array[indRow].length - 1) {
                    System.out.println();
                }
            }
            System.out.println("\t");
        }
            for(int k = 0; k < array.length; k  ){
                int i=0;
                if(isLetterPal(array)){
                    System.out.println("The word "   array[k][i]   " is a palindrome.");
                }else{
                    System.out.println("The word "   array[k][i]   " is not a palindrome.");
                }
            }
        }
    }

`

The code is what I currently have right now and the output I an getting is this enter image description here

If there will be someone who can enlighten me, I would very much appreciate it!

I tried looking over the internet and other forumas as well as youtube for same concepts. I also tried frist looking to find the palindrome per column but what I am getting is what the picture above.

CodePudding user response:

public class Hard {
    public static boolean isLetterPal (String[][] array){

The above line indicated that this function is taking a two-dimensional array of Strings and giving a single yes-or-no answer. Is this intended to check if every row and column is a palindrome, and return false if any are not? Or were you planning to check each on a case-by-case basis, in which case this signature is wrong?

In other words, should:

  AA
  AB

return a single false because AB is not a palindrome, or should it return two true and two false because some rows and columns are and some are not?

You should be very clear about your answer to these before you continue.

        int i = 0;
        int j = array.length - 1;
        while (i < j) {
            if (array.length != array.length) {

The above check is nonsense - you're asking if something is not equal to itself. This will never return true, so the below return false; will never run.

                return false;
            }
            i  ;
            j--;

The rest of this loop isn't doing anything either, except advancing towards the end of the loop. So, your entire loop is doing nothing. It's actually unclear what you were trying to do.

        }
        return true;
    }

    public static void main(String[] args) {
        
        String[][] array = { { "A", "B", "A", "B", "A" },
                             { "B", "B", "A", "B", "A" },
                             { "A", "B", "A", "B", "B" },
                             { "B", "A", "B", "B", "A" } };
        System.out.println("The array is: ");
        for (int indRow = 0; indRow < array.length; indRow  ) {
            for (int indCol = 0; indCol < array[indRow].length; indCol  ) {
                System.out.print(array[indRow][indCol]   " ");
                if (indCol == array[indRow].length - 1) {
                    System.out.println();
                }
            }
            System.out.println("\t");
        }

Check out Arrays.toString(array) to make your life easier.

            for(int k = 0; k < array.length; k  ){
                int i=0;
                if(isLetterPal(array)){
                    System.out.println("The word "   array[k][i]   " is a palindrome.");

Here you're printing out an exact square coordinate - as everything you have is length 1, this doesn't make sense. You would need to be displaying the entire row (that's array[k]) or the entire column (which is more complicated).

                }else{
                    System.out.println("The word "   array[k][i]   " is not a palindrome.");
                }
            }
        }
    }

You should break this up into smaller, more manageable sub-problems. Try making and testing the following individually:

  • A method to test if a string is a palindrome
  • A method to get a string from a given row of a 2d array
  • A method to get a string from a given column of a 2d array

Once you have those, look at how to stitch them together to answer the problem as a whole. Each of those should be much more approachable, and once you have working (tested!) solutions to each, you can treat them as bricks to build the whole.

  • Related