java - NullPointerException initializing variable with string length -
we told implement method detect if string palindrome iteration , recursion. successful in implementing method iteration getting error when try , recursively.
exception in thread "main" java.lang.nullpointerexception @ recursion.recursivepallindrome.ispallindrome(recursivepallindrome.java:14) @ recursion.recursivepallindrome.main(recursivepallindrome.java:44)
i'm clueless on has gone wrong.
package recursion; public class recursivepalindrome { static string word; public recursivepalindrome(string a) { = word; } public static boolean ispalindrome() { int start = 0; int end = word.length()-1; //line 14 char = word.charat(start); char z = word.charat(end); character.tolowercase(a); character.tolowercase(z); if(start >= end) { if(character.isletter(a) && character.isletter(z)) { if(a == z) { a++; z--; } else return false; } else if(!character.isletter(a)) start++; else if(!character.isletter(z)) end--; else return false; } return true; } public static void main(string[] args) { new recursivepalindrome("testing"); if(ispalindrome()) system.out.println("is!"); else system.out.println("is not!"); } }
try:
public recursivepalindrome(string a) { word = a; }
this code works, it isn't recursion!
package recursion; public class recursivepalindrome { static string word; public recursivepalindrome(string a) { word = a; } public static boolean ispalindrome() { int start = 0; int end = word.length() - 1; char = word.charat(start); char z = word.charat(end); character.tolowercase(a); character.tolowercase(z); while (start < end) { if (character.isletter(a) && character.isletter(z)) { if (a == z) { start++; end--; = word.charat(start); z = word.charat(end); } else { return false; } } else if (!character.isletter(a)) { start++; } else if (!character.isletter(z)) { end--; } else { return false; } } return true; } public static void main(string[] args) { new recursivepalindrome("testing"); if (ispalindrome()) system.out.println("is!"); else system.out.println("is not!"); } }
Comments
Post a Comment