Re: Problems with recursion
I made some corrections to the code and have posted the new code below. The program runs correctly if I input a string that does not contain any A's, T's, C's or G's, but still does not run properly if I input something correct such as "AATT" (without the quotes). I think the problem is somewhere in the second conditional of the user-defined method, checkPalindrome.
Code :
import java.util.Scanner;
public class bioPalindrome {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Please enter the DNA string to be examined."); // prompting user for the dna string.
String dnaString = console.nextLine(); // storing the input in to the variable dnaString
dnaString = dnaString.toUpperCase(); // converting the characters to upper case.
System.out.println(dnaString + " " + dnaString.length()); // to check that the variables are being properly initialized...remove later.
boolean isPalindrome = checkPalindrome(dnaString, 0, dnaString.length()); // calling the method, checkPalindrome, where 0 is the position of the first character of the string and stringLength is the last character of the string.
if(isPalindrome == true)
System.out.println("The DNA string entered is a biopalindrome.");
else
System.out.println("The DNA string entered is not a biopalindrome.");
}
//-----------------------------------------------------------------------------------------------------------------------------------
// User defined recursive method to check that the input is a biopalindrome.
public static boolean checkPalindrome(String stringIn, int start, int end) {
if(stringIn.length() - 1 == 0) // base case
return true;
else if(stringIn.charAt(start) == 'A' && stringIn.charAt(end - 1) == 'T' || stringIn.charAt(start) == 'T' && stringIn.charAt(end - 1) == 'A' || stringIn.charAt(start) == 'C' && stringIn.charAt(end - 1) == 'G' || stringIn.charAt(start) == 'G' && stringIn.charAt(end - 1) == 'C') { // general case
start++;
end--;
return checkPalindrome(stringIn, start, end);
} else
return false;
}
}
If I input "AATT", I receive the following error when running the program: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:558)
at bioPalindrome.checkPalindrome(bioPalindrome.java:4 0)
at bioPalindrome.checkPalindrome(bioPalindrome.java:4 3)
at bioPalindrome.checkPalindrome(bioPalindrome.java:4 3)
at bioPalindrome.checkPalindrome(bioPalindrome.java:4 3)
at bioPalindrome.checkPalindrome(bioPalindrome.java:4 3)
at bioPalindrome.main(bioPalindrome.java:24)
Re: The code seems to work now
I made a small change and the code seems to work now. The change I made is found within the first conditional statement of the checkPalindrome user-defined method. The code did read:
Code :
if(stringIn.length() - 1 <= 0 && start == end)
and now the code reads
Code :
if(/*stringIn.length() - 1 <= 0 &&*/ start == end)
I just commented out the string.length() - 1. The program works with the given string that we are to examine with the program. I'm not sure if it'll work for all odd-numbered strings. I'll post back if I find that the code is not general enough.
Re: Problems with recursion
If you want it to be general, change it to this:
Re: Problems with recursion
Yes, you're right, helloworld922. I've made the changes. Thank you for your response.