Picklejar 75
Picklejar 75

Reputation: 37

Unresolved variable error

I am writing a small program to tell if an array of strings reads the same forwards and backwards. Right now my program should return false. I am having a bit of difficulty because when i scan the array I want the second forloop to scan the same index that the first for loop is on but I am getting an unresolved variable error. I knw there is a more simple solution but I just want to find a fix for this particular error.

public class Pal {
 public static void main(String[] args) {
  String[] pal = {
   "alpha",
   "beta",
   "gamma",
   "delta",
   "gamma",
   "beta",
   "alpha",
   "donw"
  };
  System.out.println(palindrome(pal));
 }

 public static boolean palindrome(String[] pal) {
  String[] container = new String[pal.length];
  String[] rcontainer = new String[pal.length];
  for (int i = 0; i <= pal.length - 1; i++) {
   container[i] = pal[i];
  }
  for (int k = pal.length - 1; k >= 0; k--) {
   rcontainer[k] = pal[i];
  }
  for (int a = 0; a < pal.length; a++) {
   if (rcontainer[a].equals(container[a])) {
    return true;
   }
  }

  return false;
 }

}

Upvotes: 0

Views: 1219

Answers (2)

omt66
omt66

Reputation: 5019

You have a problem with local variable 'i' since it wasn't in the scope, the compiler will issue an error and the code won't compile! Also, you need to check your algorithm. Based on what you want to do, I believe I come up with a simple solution that you may want to take a look at. Hope it will be helpful.

package palindrome;

public class Pal {
    public static void main(String[] args) {
        String[] pal1 = {"alpha", "beta", "gamma", "delta", "gamma", "beta", "alpha", "donw"};
        String[] pal2 = {"aaa", "aka", "eve", "otto", "abcdefgfedcba"};
        System.out.println(palindrome(pal1)); // false
        System.out.println(palindrome(pal2)); // true
    }

    public static boolean isPalindrome(String word) {
        StringBuffer sb = new StringBuffer();
        int len = word.length();

        while (--len >= 0) {
            sb.append(word.charAt(len));
        }

        String reverse = sb.toString();

        if (word.equals(reverse)) {
            return true;
        }

        return false;
    }

    public static boolean palindrome(String[] pal) {
        int len = pal.length;

        for (int i = 0; i < len; i++) {
            String word = pal[i];
            if (isPalindrome(word) != true) {
                return false;
            }
        }

        return true;
    }
}

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

i in rcontainer[k]=pal[i]; is undefined.

Changing the line to rcontainer[k]=pal[k]; will remove the error, but the logic to judge if the input is palindrome is also wrong. You have to

  • Reverse one of the array instead of creating two copies of arrays with same contents.
  • Check if all of elements in the original and reversed array is same, not one of them is.

improved code:

public class Pal {
    public static void main (String[]args) {
        String[] pal= {"alpha", "beta", "gamma", "delta", "gamma", "beta","alpha","donw"};
        System.out.println(palindrome(pal));
    }

    public static boolean palindrome(String[]pal) {
        String[]container=new String[pal.length];
        String[]rcontainer=new String[pal.length];
        for (int i=0;i<=pal.length-1;i++) {
            container[i]=pal[i];
        }
        for (int k=pal.length-1;k>=0;k--) {
            rcontainer[k]=pal[pal.length-1-k];
        }
        for (int a=0;a<pal.length;a++) {
            if(!rcontainer[a].equals(container[a])) {
                return false;
            }
        }

        return true;
    }

}

Note that the original array is the original array, so you need not copy the original array and can just use pal instead of container.

Upvotes: 1

Related Questions