# Read strings, calculate Hamming Distance

• April 23rd, 2013, 06:22 AM
dianac
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

Code java:

```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?
• April 23rd, 2013, 06:50 AM
Norm
Re: Read strings, calculate Hamming Distance
Quote:

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
• April 23rd, 2013, 09:27 PM
dianac
Re: Read strings, calculate Hamming Distance
Thanks. But how do I refer to characters in the arguments?
Code java:

``` 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?
• April 23rd, 2013, 10:13 PM
pbrockway2
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.
• April 24th, 2013, 02:44 AM
zoidberg
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.
• April 24th, 2013, 03:05 AM
PhHein
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.
• April 24th, 2013, 10:22 PM
dianac
Re: Read strings, calculate Hamming Distance
When I try:
Code java:

```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?
• April 24th, 2013, 10:26 PM
pbrockway2
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.
• April 24th, 2013, 10:33 PM
dianac
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.
• April 25th, 2013, 12:07 AM
javanoob360
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 ;)
• April 25th, 2013, 12:24 AM
dianac
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!!
• April 25th, 2013, 01:52 AM
pbrockway2
Re: Read strings, calculate Hamming Distance
Quote:

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.