Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2011
    Rep Power

    Default Help with String Manipulation

    I am taking an introductory university course on Java. Today in lab we were dealing with string manipulation, specifically how to take a large string (in our case the genetic code for H1N1, represented in characters), and determine how often a specific substring occurs within the larger string. After 2 hours of trying to figure out what was wrong with this code, I, my two TAs, and a grad student that happened to be in the room at the time were left baffled. For some reason, some of the numbers are correct, and at other times they are a few counts short of what they should be (searching "tag," for example, should return 10, but instead returns 8). I even tried entering it into a different IDE, with no change in the outputs. Any help or hints is greatly appreciated by everyone involved in this mess.

    (Here is the code I was using.)
    Java Code:
    import java.util.*;
    public class StringManip {
    	public static void main(String[] args) {
    		Scanner s = new Scanner(;
    		boolean done = true;
    		int count = 0, index = 0;
    		String sequence = "gcgtcattacttatgaaaacaacacttgggtaaatcagacatatgttaacatcagcaacaccaactttgctgctggacagtcagtggtttccgtgaaattagcgggcaattcctctctctgccctgtgtggatgggctatatacagtaaagacaacagtgtaagaatcggttccaagggggatgtgtttgtcataagggaaccattcatatcatgctcccccttggaatgcagaaccttcttcttgactcaaggccttgctaaatgacaaacattccaatggaaccattaaagacaggagcccatatcgaaccctaatgagctgtcctattggtgaagttccctctccatacaactcaagatttgagtcagtcgcttggcacaagtgcttgtcatgatggcatcaattggctaacaattggaatttctggcccagacaatggggcagtggctgtgttaaagtacaacggcataataacagacactatcaagagttggagaaacaaatatgagaacacaagagtctgaatgtgcatgtgtaaatggttcttgctttactgtaatgaccgatggaccaagtaatggacaggcctcatacaagatcttcagaatagaaaagggaaagatagtcaatcatcgaaatgaatgcccctaattatcactatgaggaatgctcctgttatcctgattctggtgaaatcacatgtgtgtgcagggataactggcatggctcgaatcgaccgtgggtgtctttcaacagaattggaatatcagataggatacatatgcagtgggattttcggagacaatccacgccctaatgacaagacaggcagttgtggtccagtatcgtctaatggagcaaatggagtaaaaggattttcttcaaaacggcaatggtgtttggatagggagaactaaaagcattagttcaagaaacggttttgagatgatttgggatccgaacggatggactgggacagacaataacttctcaataaagcaagatacgtaggataaatgagtggtcaggatatagcgggagttttgttcagcatccagaactaacagggctggattgtataagaccttgcttctgggttgaactaatcagagggcgacccaaagagaacacatctggactgcgggagcagcatatccttttgtggtgtaaacagtgacactgtgggttggtcttggccagacggtgctgagt";
    		int totalLength = sequence.length();
    		int x = 0;
                    System.out.print("SEARCH: ");
    		String search =;
    		String sub = sequence;
                    while (done) {
    			index = sub.indexOf(search);
    			if (index == -1) {
    				done = false;
    			else {
    				count += 1;
    			index += 1;
    			sub = sub.substring(index);
    		System.out.println("Sequence " + search + " appears " + count + " times.");

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Help with String Manipulation

    I think that Java is right and that you , your TA, and the random guy are wrong. The correct answer for the sequence "tag" is in fact 8.

  3. #3
    Join Date
    Sep 2011
    Rep Power

    Default Re: Help with String Manipulation

    Thanks, I actually just got an email at about the same time as you replied from the grad student, saying that when she typed the big string into Word and used the "find" function there were only 8 instances of "tag" in the string. I'm not sure how/why the string was truncated when it was copied into my code or if the instructor simply gave us an incorrect sample output, but it's good to know that I'm not quite as incompetent as I was beginning to believe! Thanks for the help.

Similar Threads

  1. String manipulation
    By lakshmibvaraprasad in forum New To Java
    Replies: 4
    Last Post: 07-16-2011, 01:41 PM
  2. String manipulation
    By i4ba1 in forum Advanced Java
    Replies: 2
    Last Post: 12-22-2010, 08:53 AM
  3. String Manipulation
    By tmotse in forum New To Java
    Replies: 1
    Last Post: 10-14-2010, 01:25 PM
  4. Help needed with string manipulation
    By stringkilla in forum New To Java
    Replies: 47
    Last Post: 09-07-2010, 03:25 AM
  5. String Manipulation Task
    By hiranya in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 11:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts