Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Threaded View

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problems with recursion

    Hey guys,

    I am currently enrolled in a CS2 class and am working on a problem involving recursion. The problem is similar to checking that a string is a palindrome, but it's a little different. In this program, A's pair with T's and C's pair with G's. Given an expression, such as ACGCGT, the program must check that each character has an appropriate corresponding character. So the first character is an A and the last character is a T, the second character is a C and the second to last character is a G, and so on. The above expression meets these conditions. For this assignment, the expression will contain an even number of characters.

    The code 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.
    		int stringLength = dnaString.length(); // storing the length of the string into the variable stringLength.
    		System.out.println(dnaString + " " + stringLength); // to check that the variables are being properly initialized...remove later.
     
    		boolean isPalindrome = checkPalindrome(dnaString, 0, stringLength - 1); // calling the method, checkPalindrome, where 0 is the position of the first character of the string and stringLength - 1 is the  last character of the string.
    		System.out.println(isPalindrome);
     
    	}
     
    	//-----------------------------------------------------------------------------------------------------------------------------------
    	// 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() == 0) // base case
    			return true;
    		else if(stringIn.charAt(start) == 'A' && stringIn.charAt(end) == 'T' || stringIn.charAt(start) == 'T' && stringIn.charAt(end) == 'A' || stringIn.charAt(start) == 'C' && stringIn.charAt(end) == 'G' || stringIn.charAt(start) == 'G' && stringIn.charAt(end) == 'C') { // general case
    			start++;
    			end--;
    			return checkPalindrome(stringIn, start, end);
    		} else
    			return false;
    	}
    }

    This compiles, but when I try to run the program with an expression like, AATT, I get this message: 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:3 5)
    at bioPalindrome.checkPalindrome(bioPalindrome.java:3 8)
    at bioPalindrome.checkPalindrome(bioPalindrome.java:3 8)
    at bioPalindrome.checkPalindrome(bioPalindrome.java:3 8)
    at bioPalindrome.checkPalindrome(bioPalindrome.java:3 8)
    at bioPalindrome.main(bioPalindrome.java:24)

    I think what I need to do is use the substring method, but this introduces problems in the parameter list of the method. For example, instead of passing a string, and two int values, when calling the method, I tried to pass checkPalindrome(dnaString.substring(0, stringLength - 1)), but it won't allow this in the user - defined method where the parameter list will be public static boolean checkPalindrome(String stringIn.substring(int a, int b)). I get a "multiple markers at this line error".

    I appreciate your help. Thanks.

    I am using Eclipse Version 3.5.0
    Mac OS X version 10.5.8
    Java version Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0.20)
    Last edited by KingLane; September 20th, 2009 at 07:21 PM.


Similar Threads

  1. Replies: 4
    Last Post: September 19th, 2009, 11:56 PM
  2. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM
  3. big problems bounding() in Java2d with LineBreakMeasurer.
    By fenderman in forum AWT / Java Swing
    Replies: 0
    Last Post: July 27th, 2009, 01:15 PM
  4. Problems in setting classpath
    By missyati in forum Java Theory & Questions
    Replies: 3
    Last Post: June 30th, 2009, 12:43 AM
  5. Replies: 1
    Last Post: April 1st, 2009, 02:47 PM

Tags for this Thread