finger.k4
Intern
Код:
public class ShortestPalindrom
{
public static void main (String [] args)
{
String [] test1 = {"aba", "abba", "xxxx"};
String [] test2 = {"ab", "aba"};
String [] test3 = {"xyzzyx", "abc", "cde"};
String [] test4 = {"abc", "cde"};
System.out.println ("The shortest palindrome in test 1 is " + shortest (test1));
System.out.println ("The shortest palindrome in test 2 is " + shortest (test2));
System.out.println ("The shortest palindrome in test 3 is " + shortest (test3));
System.out.println ("The shortest palindrome in test 4 is " + shortest (test4));
}
static String shortest(String[] words)
{
boolean isPalindrome = false;
String palindromeToReturn = "";
if (words.length > 20)
return "The array passed exceeds the limit of 20 words!";
for (int i = 0; i < words.length; i++)
{
if (words[i].length () > 1 && words[i].length () <= 50)
{
for (int j = 0; j < words[i].length (); j ++)
{
for (int z = words[i].length () ; z > 0; z--)
{
if (words[i].charAt (j) == words[i].charAt (z))
isPalindrome = true;
else
isPalindrome = false;
}
}
if (isPalindrome == true)
palindromeToReturn = words[i];
}
else
break;
}
return palindromeToReturn;
}
}
mi dava error koga go run-uvam a ne koga kompajliram...vika StringIndexOutOfBouds vo linija 30...
e sea verojatno e povrzano toa nesto so charAt () momentite ama vrska neam sto???
hELP?