Click here to Skip to main content
15,892,746 members
Home / Discussions / Java
   

Java

 
QuestionN Queens Problem help Pin
shiggedyshwa21-Sep-09 19:20
shiggedyshwa21-Sep-09 19:20 
AnswerRe: N Queens Problem help Pin
427748021-Sep-09 20:27
427748021-Sep-09 20:27 
GeneralRe: N Queens Problem help Pin
shiggedyshwa21-Sep-09 21:15
shiggedyshwa21-Sep-09 21:15 
QuestionRead a file and find text Pin
Mbu00720-Sep-09 22:28
Mbu00720-Sep-09 22:28 
AnswerRe: Read a file and find text Pin
427748021-Sep-09 4:31
427748021-Sep-09 4:31 
QuestionHow do i get all values from a JTextfields that is located on another JInternalFrame? Pin
miceisland20-Sep-09 20:36
miceisland20-Sep-09 20:36 
AnswerRe: How do i get all values from a JTextfields that is located on another JInternalFrame? Pin
427748021-Sep-09 4:37
427748021-Sep-09 4:37 
QuestionHaving trouble with recursion (kind of long, but please read) Pin
KingLane20-Sep-09 12:48
KingLane20-Sep-09 12:48 
Hey guys,

I am working on an assignment that concerns itself with recursion. My program compiles, but does not run properly. The program is similar to checking that a string is a palindrome, but it is a little different. This program is supposed to check that each character in the string has a correct corresponding character. For example, in this assignment A's pair with T's and C's pair with G's. So the input AATT should be evaluated as a biopalindrome. My program runs correctly if I input a string that does not contain any A's, T's, C's or G's, but if I input the above expression, AATT, the error message I receive when the program runs is this:



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:42)
at bioPalindrome.checkPalindrome(bioPalindrome.java:45)
at bioPalindrome.checkPalindrome(bioPalindrome.java:45)
at bioPalindrome.checkPalindrome(bioPalindrome.java:45)
at bioPalindrome.checkPalindrome(bioPalindrome.java:45)
at bioPalindrome.main(bioPalindrome.java:24)


Here is the code that I have so far:


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) {
		/*System.out.println(start + " " + end); // to verify that the values are being passed properly.
		stringIn = stringIn.substring(start, end);
		System.out.println(stringIn);*/
		if(stringIn.length() - 1 == 0 && start == end) // 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;
	}
}


I appreciate any help or suggestions anyone can offer. Thanks again for your help.
AnswerI got the code to work Pin
KingLane20-Sep-09 14:06
KingLane20-Sep-09 14:06 
QuestionJava projects Pin
prietycool19-Sep-09 0:29
prietycool19-Sep-09 0:29 
AnswerRe: Java projects Pin
Richard MacCutchan20-Sep-09 9:29
mveRichard MacCutchan20-Sep-09 9:29 
QuestionJava web Project using Springs Pin
prietycool19-Sep-09 0:27
prietycool19-Sep-09 0:27 
AnswerRe: Java web Project using Springs Pin
427748021-Sep-09 5:22
427748021-Sep-09 5:22 
QuestionMerge Sort problem Pin
snssewell18-Sep-09 18:02
snssewell18-Sep-09 18:02 
AnswerRe: Merge Sort problem Pin
427748018-Sep-09 18:13
427748018-Sep-09 18:13 
GeneralRe: Merge Sort problem Pin
snssewell18-Sep-09 18:25
snssewell18-Sep-09 18:25 
GeneralResolved Pin
snssewell18-Sep-09 18:29
snssewell18-Sep-09 18:29 
GeneralRe: Merge Sort problem Pin
427748018-Sep-09 18:30
427748018-Sep-09 18:30 
GeneralRe: Another Problem now Pin
snssewell18-Sep-09 18:46
snssewell18-Sep-09 18:46 
AnswerRe: Merge Sort problem Pin
427748018-Sep-09 18:59
427748018-Sep-09 18:59 
GeneralRe: Finally resolved..I hope..lol Pin
snssewell18-Sep-09 19:20
snssewell18-Sep-09 19:20 
QuestionOutput String returned from encryption/decryption Fuction into a text file???Help please!!!! Pin
ChiSmile18-Sep-09 16:36
ChiSmile18-Sep-09 16:36 
AnswerRe: Output String returned from encryption/decryption Fuction into a text file???Help please!!!! Pin
427748018-Sep-09 17:54
427748018-Sep-09 17:54 
Questionhow to move cookie value from https to http page? [modified] Pin
sangeethanarayan17-Sep-09 21:54
sangeethanarayan17-Sep-09 21:54 
AnswerRe: https to http Pin
Nagy Vilmos17-Sep-09 21:57
professionalNagy Vilmos17-Sep-09 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.