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 12 of 12

Thread: Read strings, calculate Hamming Distance

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Read strings, calculate Hamming Distance

    Hi, I am trying to learn java and would like some help in:

    1.Read in a list of strings of chracters A,C,G,and T (ignoring case). Ignore the string if it contains any other character.
    2.Store the strings in an array
    3.Using a separate method, calculate the number of positions in each pair of sequences that have a different nucelotide (the 4.Hamming Distance)
    5.Print out the maximum Hamming Distance between any two sequences


    public class Hamming {
     
        /**
         * @return true if and only if every character in the input String s is one of a, A, c, C, g, G, t or T.
         * @return false if s is null or empty.
         */
        public static boolean isDNASequence(String s) {
            // TODO fill in this method
        }
        /**
         * Get the distance matrix 
         * 
         * @param sequences - array of sequences
         * @return distance matrix
         */
        public static int[][] getDistances(String[] sequences) {
            // TODO fill in this method
        }
     
        /**
         * Get the hamming distance between two string sequences
         * 
         * @param sequence1 - the first sequence
         * @param sequence2 - the second sequence
         * @return the Hamming distance
         */
        public static int getHammingDistance(String sequence1, String sequence2){
            // TODO fill in this method
        }
        /**
         * Main method
         * 
         * 1. Go through the parameters
         * 2. Ensure they are valid DNA sequences
         * 3. Get the Hamming distance matrix
         * 4. Print out the highest distance only
         * @param args - program arguments
         */
        public static void main(String[] args) {
            // TODO fill in this main method code below this line
        }
    }

    How can I code the program to read the arguments ignoring characters that are not A,C, T or G?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,735
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Read strings, calculate Hamming Distance

    ignoring characters that are not A,C, T or G?
    Use an if statement to detect the characters to be ignored and ignore them.
    if char is one of the desired 4
    then use it
    else ignore it
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Read strings, calculate Hamming Distance

    Thanks. But how do I refer to characters in the arguments?
            if(args[]:s=="A"||"c"||"C"||"g"||"G"||"t"||"T"){
        }

    This code is not right, I actually want to refer to characters inside each argument. How can I do it?

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Read strings, calculate Hamming Distance

    Strictly speaking you weren't asked to ignore nonbase characters, you were asked to asked to ignore strings that contained such characters.

    You can test if some character ch is in a String str with the String method str.indexOf(ch). If you don't care about case a common "trick" is to say str.toLowerCase().indexOf(ch).

    These String methods (or, possibly more efficient others) can be used to implement the isDNASequence() method. And the isDNASequence() method thus implemented is what you use for (1.) in the main() method. But work one step at a time: get isDNASequence() implemented and tested.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    dianac (April 24th, 2013)

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read strings, calculate Hamming Distance

    I'm in the same class as the OP, though I don't know him/her. I must say this is the only forum I've visited where people can come in and paste the skeleton code the lecturer gave us, and just ask how to do it without offering any evidence that they've thought it through, and not be flamed for it.

    I don't know if that's a good thing or a bad thing.

    *Edit* I'm not trying to come off as bitchy, it just bugs me when people don't want to figure something out for themselves.

  7. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: Read strings, calculate Hamming Distance

    Well, the OP doesn't get flamed, but he/she won't be spoon fed either. So in the end the OP has to do the assignment themself, maybe with a little help.

  8. The Following User Says Thank You to PhHein For This Useful Post:

    dianac (April 24th, 2013)

  9. #7
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Read strings, calculate Hamming Distance

    When I try:
    for(String s:args){
        		if (s.toLowerCase().indexOf("a"){    	
        	}

    It keeps returning error for "a" and it just say "no suggestions available". Do you know what I am doing wrong?

  10. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Read strings, calculate Hamming Distance

    You are passing the indexOf() method a String, "a". Read the documentation linked to for indexOf() to see what sort of thing the argument should be.

  11. #9
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Read strings, calculate Hamming Distance

    It should be a character, but when I put just a it returns an error saying I should declare variable a.

  12. #10
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Read strings, calculate Hamming Distance

    I'm in your class as well dianac and someone on reddit just helped me with this so im going to help you!
    public static boolean isDNASequence(String s){
    boolean x = false
    if(s.matches("[acgt]*")){
    x = true;
    }
    return x
    }

    try something like that and it should work, this is only a test for if the string contains only those letters by the way not for all the other conditions
    good luck with the rest of task 3

  13. The Following User Says Thank You to javanoob360 For This Useful Post:

    dianac (April 25th, 2013)

  14. #11
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Read strings, calculate Hamming Distance

    Hi there, thank you. I had already tried this one before. I think I am not reading in the arguments to a string correctly. Good Luck to you too!!

  15. #12
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Read strings, calculate Hamming Distance

    It should be a character, but when I put just a it returns an error saying I should declare variable a.
    Yes, it should be a character. Remember that character lliterals have single quote marks around them like 'a' which is quite a different thing than a variable like a

    ---

    If you've covered regular expressions in class, javanoob's suggestion might make sense. Otherwise ignore it, as you're liable to choke on the spoon.

Similar Threads

  1. Read and write to a text file and calculate values
    By s.sariyan in forum What's Wrong With My Code?
    Replies: 19
    Last Post: November 28th, 2012, 09:19 AM
  2. Distance function help
    By abf617 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 06:32 PM
  3. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 21st, 2012, 11:53 PM
  4. Read Text File Into Strings
    By Khadafi in forum Java Theory & Questions
    Replies: 31
    Last Post: January 14th, 2012, 08:29 AM
  5. NullPointerException when trying to read Strings into an array of myObject
    By sdivinyi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 24th, 2010, 09:45 AM